I want to get table name from model in yii2.
I tried to call the function below in vain :
public static function tableName()
{
return '{{%user}}';
}
Thanks for help.
Try this -
ModelNameGoesHere::model()->tableSchema->name;
Replace ModelNameGoesHere with your model's name.
Related
I have 2 tables employees and employee_locations. One employee has many locations. I need to find out one employee record with related latest employee_locations record. I wrote below query.
$employees = Employee::find([1])->with('employees.employee_locations')->latest()->first();
I am getting below error
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::with does not exist.
Your issue is that the find method retrieves a collection of Eloquent objects, on which the with method can not be used. You must first specify your relations for the Employee objects and then use find.
The below code will retrieve the employee with the ids specified in find method and locations for each employee:
$employees = Employee::with('employees.employee_locations')->find([1])
create a relation in your model. Something like this:
class Employee extends Model
{
protected $table = 'employees';
public function location()
{
return $this->hasMany(EmployeeLocation::class, 'employeed_id');
}
}
class EmployeeLocation extends Model
{
protected $table = 'employee_locations';
}
$employees = Employee::with('location')->first();
or you do
$employees = Employee::with('location')->find(<employeed_id>);
Try This method
$employees = Employee::with('employees')->where('employeesid',$employeesid)-
>get()->find($employeesid);
I have a model Work with this relation
public function types()
{
return $this->belongsTo('App\Models\Type');
}
And a model Type with this relation
public function works()
{
return $this->hasMany('App\Models\Work');
}
I try to access in my view show view to type but I've a lot of errors
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name
I try this : $work->types()->name for get data.
In my DB, my table 'Works' have a foreignkey 'type_id'.
I would like to get the 'type' of the post. There can be only one per post.
Thank you very much !
Semantically you want to make your relationships like so:
Work
// A work is of a single type
public function type()
{
return $this->belongsTo('App\Models\Type');
}
Type
// A type of work can have many items of work
public function works()
{
return $this->hasMany('App\Models\Work');
}
You can then access the relationship like so:
$type = Work::first()->type // return object of type Type
$works = Type::first()->works // return collection of objects of type Work
EDIT
By accessing the relationship with () you are returning the underlying query builder instance of the relationship and you will need to finish your statement with ->get() like so:
$works = Type::first()->works()->get();
You should have on Work Model:
public function type()
{
return $this->belongsTo('App\Models\Type');
}
and on your view:
$work->type->name;
Since you are not using default id as foreign key you should add
protected $primaryKey = "type_id";
in your model
I have Model_Group that extends ORM.
I have Controller_Group that gets a new ORM:
public function before()
{
global $orm_group;
$orm_group = ORM::factory('Group');
}
...and it has various methods that use it to get different subsets of data, such as...
public function action_get_by_type()
{
global $orm_group;
$type = $this->request->param('type');
$result = $orm_group->where('type', '=', $type)->find_all();
}
Then I have another controller (in a separate module) that I want to use to manipulate the object and call the relevant view. Let's call it Controller_Pages.
$orm_object = // Get the $result from Controller_Group somehow!
$this->template->content = View::factory( 'page1' )
->set('orm_object', $orm_object)
What is the best way to pass the ORM object from Controller_Group to Controller_Pages? Is this a good idea? If not, why not, and what better way is there of doing it?
The reason for separating them out into different controllers is because I want to be able to re-use the methods in Controller_Group from other modules. Each module may want to deal with the object in a different way.
This is the way I would do it, but first I would like to note that you shouldn't use global in this context.
If you want to set your ORM model in the before function, just make a variable in your controller and add it like this.
public function before()
{
$this->orm_group = ORM::factory('type');
}
In your Model your should also add the functions to access data and keep the controllers as small as possible. You ORM model could look something like this.
public class Model_Group extends ORM {
//All your other code
public function get_by_type($type)
{
return $this->where('type', '=', $type)->find_all();
}
}
Than in your controllers you can do something like this.
public function action_index()
{
$type = $this->request->param('type');
$result = $this->orm_group->get_by_type($type);
}
I hope this helps.
I always create an helper class for stuff like this
Class Grouphelper{
public static function getGroupByType($type){
return ORM::factory('Group')->where('type','=',$type)->find_all();
}
}
Now you're been able to get the groups by type where you want:
Grouphelper::getGroupByType($type);
Sorry if I am using wrong keyword. Might be I did not get sufficient information due to wrong keyword.
we create widget in Yii by this way:
class streamList extends CWidget {
//do some stuff
}
Also, we use this widget anywhere as
$this->widget("application.components.widget.streamList");
How can we write the widget in such a way that it accepts parameter(s) as
$this->widget("application.components.widget.streamList",array('title'=>'Posts');
I googled but did not solve. Please help, thank in advance.
Edit log:
Also, how can we define default parameter value to 'titile'? I tried public $title = Shk::getTitle(), but it did not work.
Use
class StreamList extends CWidget {
//do some stuff
public $title;
}
Any attributes can be initialized with default values and overwritten by
$this->widget("application.components.widget.StreamList",array('title'=>'Posts',.....)
EDIT
You can't initialize class attributes with functions. An explanation is given here. An option is to check whether $title is set and if not set it to Shk::getTitle() in the init() method like
public function init(){
....
if(!isset($this->title) || !$this->title)
$this->title=Shk::getTitle();
....
}
P.S for consistency it's better to Capitalize your class names.
Hi there i am creating some classes and methods for my project so that it can be used over and over
now the problem is this that is has lots of functions in these dlls. i want to give them some name as alias name so that user can easily find and call these function as per their requirements ..
is there any way to call a function with two or more name . if any one please give me ans....
Thanks
In what language?
In C#, creating an alias to a method is as easy as:
public class Demo
{
public int SomeMethodHere(string argument)
{
// Code here.
}
public int AliasToSomeMethod(string argument)
{
return this.SomeMethodHere(argument);
}
}
Do the functions need to be overridable?
If not just have another function call the alias that calls the main function.