I have one CompanyConroller connect with companies table.
how can i get companies table value via calling from SubadminController?
You can get companies table value by calling SubadminController by giving this relation in Subadmin model
var $belongsTo = array("Company");
or
var $hasMany = array("Company");
Related
i have relationship many to many table 'admins' , 'pjt_roles' with pjt_role_admin.
but,not working
i have 2 model
class Role
protected $table = 'pjt_roles';
public function Admin(){
return $this->belongsToMany(Admin::class',pjt_role_admin');
}
class Admin
public function Role(){
return $this->belongsToMany(Role::class,'pjt_role_admin');
}
and table pjt_role_admin have attribute
admin_id from table admins
role_id from table pjt_roles
Specify your pivot table in relationship. Default laravel assume admin_role as your pivot table because you have Admin and Role models
class Role
protected $table = 'pjt_roles';
public function Admin(){ // should be admins() for better readability
return $this->belongsToMany(Admin::class, 'pjt_role_admin');
}
class Admin
public function Role(){ // should be roles() for better readability
return $this->belongsToMany(Role::class, 'pjt_role_admin');
}
To determine the table name of the
relationship's joining table, Eloquent will join the two related model
names in alphabetical order. However, you are free to override this
convention. You may do so by passing a second argument to the
belongsToMany method.
Fetch Data
$admin = Admin::find(1);
$roles = $admin->Role; // should change to roles() in relationship for better readability
Save
$admin->Role()->attach($roleId);
details https://laravel.com/docs/5.4/eloquent-relationships#many-to-many
I am using 'WhoDidiT' in my model to insert user_id. Its working fine.
Now i need to insert a different user_id instead of the logged in user_id.
Is there any way to insert the different user_id without modifying 'WhoDidiT'
var $actsAs = array(
'WhoDidIt'=>array(
'created_by_field'=>'created_by',
'modified_by_field'=>'modified_by'
),
);
After you save, you can update that specific field
$this->Model->saveField('created_by',$id);
this will do the trick
I'm new to cakePHP and I'm having issues getting all of my models to link up correctly.
This is the layout of my models in cakePHP:
Class belongsTo Location
Class belongsTo ClassType
Class hasMany ScheduledClass
ScheduledClass belongsTo Instructor
My problem is that when I use:
$this->Class->find('all')
I only get data from the Class, Location, ClassType and ScheduledClass models. I do not get any data from the Instructor model.
I can set the recursive value to '2' and retreive the data from the Instructor model, but this results in a giant amount of queries(one for every row) instead of a join on "ScheduledClass.instructor_id = Instructor.id".
What I was hoping to acheive is something like:
SELECT
...
...
FROM classes as Class
INNER JOIN locations as Location on Class.location_id = Location.id
INNER JOIN class_types as ClassType on Class.class_type_id = ClassType.id
INNER JOIN scheduled_classes as ScheduledClass on ScheduledClass.class_id = Class.id
INNER JOIN instructors as Instructor on ScheduledClass.instructor_id = Instructor.id
I've tried using both Containable and Joins in order to get the right data, but I wasn't able to get either to work(possibly due to my misunderstanding their uses).
Thank you in advance!
use Containable and set how you want the find to react and fetch what data you want.
in the Model
var $actsAs = array('Containable');
and in the Controller you can do something like this
$this->data = $this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id')), 'contain' => array('City', 'Region', 'Country', 'UserOccupations', 'UserGroup')));
if you have your Model/Databases relationships set right you can pull the exact data you want even choosing which columns you wanna pull.
I am in a Model1 (Model1)and I need to run some business logic that should update a record in the Model2 (table2).
Here is the query/action I need to run from Model1 in Model2
$sql ="update table2 SET products = $product WHERE `id` = '".$id."'";
How would I go about doing it?
If the two models are related, you can save data to two tables at the same time. See the book for information on how to use Model::saveAll(). This is how you would represent that query though:
$this->Model2->id = $id;
$this->Model2->saveField('products', $product);
if the 2 models are not related (no relationship), Model->query() is a quick and dirty way. Otherwise, if Model1 has a relationship to Model2, in Model1, you can refer to Model2 by : $this->Model2->function_you_need_to_call()
i have theses tables
article , article_currency, currency in database
and i made HABTM between article and currency like this
var $hasAndBelongsToMany = array('currency'=>array('className' => 'currency','joinTable'=>'article_currency','foreignKey' => 'articleid','associationForeignKey' => 'CurrencyID'));
var $hasAndBelongsToMany = array('articlemodel'=>array('className' => 'articlemodel','joinTable'=>'article_currency','foreignKey' => 'CurrencyID','associationForeignKey' => 'ArticleID'));
and here cake genrate the model ArticleCurrency for me
when i try to change its name using with to 'article_currency'
is give me this error
Database table article_currencies for model article_currency was not found.
how i can solve this
Table names are plural by convention: articles, currencies, articles_currencies
http://book.cakephp.org/view/24/Model-and-Database-Conventions