i have a model that have 2 habtm association.
my models are: 'Course', 'Teacher' and 'Student.
course hasAndBelongsToMany Teacher and hasAndBelongsToMany Student.
when i coding like this:
public $hasAndBelongsToMany = 'Student';
public $hasAndBelongsToMany = 'Teacher';
Or:
public $hasAndBelongsToMany = array(
'Student' => array(
'className' => 'Student',
'joinTable' => 'courses_students',
'foreignKey'=> 'course_id',
'associationForeignKey' => 'student_id'
),
'Teacher' => array(
'className' => 'Teacher',
'joinTable' => 'courses_teachers',
'foreignKey'=> 'course_id',
'associationForeignKey' => 'teacher_id'
)
);
i see internal error
how can i code this?
Thanks
"Cannot redeclare class Course"
says it all: you probably have a copy-and-paste-error.
You declare the class wrong in your Teacher.php (should be Teacher not Course).
Related
I have two tables:
users contains (id, name, email) id is the primary key.
users_details contains (user_id, address, city, postcode) user_id is the foreign key to user table.
The users.id=users_details.user_id.
I wrote user model like this in User.php
class User extends AppModel {
public $name = 'User';
public $displayField = 'name';
public $primaryKey = 'id';
var $belongsTo = 'UsersDetail';
public $hasone = array(
'UsersDetail' => array(
'className' => 'UsersDetail',
'conditions' => '',
'fields' => '',
'foreignKey' => 'id',
'order' => '',
'dependent' => true
)
);
}
I wrote the usersdetail model in UsersDetail.php
class UsersDetail extends AppModel {
public $name = 'UsersDetail';
public $displayField = 'name';
public $belongsTo = array( 'User' =>
array( 'className' => 'User','foreignKey' => 'user_id') );
public $hasone = array(
'User' => array(
'className' => 'User',
'conditions' => '',
'fields' => '',
'foreignKey' => 'id',
'order' => '',
'dependent' => true
));
}
I want to get the data from table using the join with condition. The users.id=users_details.user_id.
You have declared two relationships for each model association, when you should only have one going each way.
Like so:
In the userDetail Model:
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
And consequently, in the User Model:
public $belongsTo = array(
'UserDetail' => array(
'className' => 'UserDetail',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Check out the documentation for more details.
user has (id, name, email) id is the primary key.
users_details has(user_id, address, city, postcode) user_id is the foreign key to user table. Means.
Point to remember:
one to one relation - hasOne - A user table has one users_detail.
belongto - A userdetails table belongs to User table.
So, the relation between the two tables is hasone and belongsto.
find the right code below.
User model:
public $hasOne = array('UsersDetail' => array(
'className' => 'UsersDetail',
'foreignKey' => 'user_id')
);
UsersDetail model
public $belongsTo = array('User' => array(
'className' => 'Users',
'foreignKey' => 'id',
'conditions' => '',
'fields' => '',
'order' => '')
);
which provide the join precisely. You can add the conditions, order in collections.
The output query
SELECT `User`.`id`, `User`.`first_name`, `User`.`last_name`, `User`.`email`, `User`.`mobile`, `User`.`pswd`, `UsersDetail`.`user_id`, `UsersDetail`.`address`, `UsersDetail`.`city`, `UsersDetail`.`state`, `UsersDetail`.`postcode`, `UsersDetail`.`dob`, `UsersDetail`.`photo` FROM `cakedemo`.`users` AS `User` LEFT JOIN `cakedemo`.`users_details` AS `UsersDetail` ON (`UsersDetail`.`user_id` = `User`.`id`) WHERE 1 = 1
Thank you,
I've run through the Cakephp documentation on linking models together (creating associations), and I can get a single select dropdown in the scaffolded add and edit views by defining a $hasOne member one model and a corresponding $belongsTo in the other. Now, for a $hasMany relationship, I'm trying to get some kind of form input (either a multi-select dropdown, or checkboxes ... preferably checkboxes) in my scaffolded add and edit views that lets the user choose the associated advertisements for a given rental. But with the below code I get no mention of Advertisements in the Rental add and edit views :'( Am I missing something? Is this even possible? I've seen this done in RoR and Grails but can't get it working with CakePHP. Thanks for any help!
Rental model (app/Model/Rental.php)
<?php
class Rental extends AppModel {
var $name = 'Rental';
var $belongsTo = array(
'Agent' => array(
'className' => 'User',
'foreignKey' => 'agent_id'
),
'Bedroom',
'Landlord' => array(
'className' => 'Landlord',
'foreignKey' => 'landlord_id'
)
);
var $hasMany = array(
'Advertisement' => array(
'className' => 'Advertisement',
'foreignKey' => 'rental_id',
//'conditions' => array('Comment.status' => '1'),
'order' => 'Advertisement.created DESC',
'limit' => '5',
'dependent'=> false
)
);
public $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'description' => array(
'rule' => 'notEmpty'
)
);
public function isOwnedBy($rental, $user) {
return $this->field('id', array('id' => $rental, 'user_id' => $user)) === $rental;
}
}
Rentals Controller (app/Controller/RentalsController.php)
<?php
class RentalsController extends AppController {
public $scaffold;
}
Advertisement model (app/Model/Advertisement.php)
<?php
class Advertisement extends AppModel {
var $name = 'Advertisement';
var $belongsTo = array(
'Rental' => array(
'className' => 'Rental',
'foreignKey' => 'rental_id'
),
'Author' => array(
'className' => 'User',
'foreignKey' => 'author_id'
)
);
}
Advertisements Controller (app/Controller/AdvertiesementsController.php)
<?php
class AdvertisementsController extends AppController {
public $scaffold;
}
Cake's scaffold views don't show anything from the hasMany side of the relationship. They assume you want to pick it from the model that has the belongsTo.
Frankly, doing it as a multi-select is a little odd for this relationship, IMHO. Multi-selects are normally used for hasAndBelongsToMany relationships. If you want to do it, you'll need to do the work yourself, and not use Cake's scaffolding.
Consider the following HABTM relation in CakePHP 2.2.3:
class User extends AppModel
{
public $hasAndBelongsToMany = array(
'Role' => array(
'className' => 'Role',
'joinTable' => 'roles_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'role_id',
)
);
}
This works fine, but when using an alias like VeryUniqueAlias instead of Role and changing the UsersController accordingly, the m:n relation is not persisted in the database (the data passed to save() in the controller are equivalent).
This does not work:
class User extends AppModel
{
public $hasAndBelongsToMany = array(
'VeryUniqueAlias' => array(
'className' => 'Role',
'joinTable' => 'roles_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'role_id',
)
);
}
This is awkward, since the the CakePHP docs state that it should work.
Any idea why it's not working for me? Did I miss something?
Use the 'with' key to define the name of the model for the join table. In your case:
public $hasAndBelongsToMany = array(
'VeryUniqueAlias' => array(
'className' => 'Role',
'joinTable' => 'roles_users',
'with' => 'RolesUser', // first model pluralized
'foreignKey' => 'user_id',
'associationForeignKey' => 'role_id',
)
);
I was following a tutorial about permissions instead CakePHP.
I don't understand how this model (query) works.
Can anybody explain me how this query works.. I can't find what this variable hasAndBelongsToMany exactly does.
<?php
class Group extends Appmodel {
var $name = 'Group';
var $useTable 'groups';
var $hasAndBelongsToMany = array(
'Permission' => array('className' => 'Permission',
'joinTable' => 'groups_permissions',
'foreignKey' => 'group_id',
'associationForeignKey' => 'permission_id',
'unique' => true
)
'User' => array('className' => 'User',
'joinTable' => 'groups_users',
'foreignKey' => 'group_id',
'associationForeignKey' => 'user_id',
'unique' => true
),
);
}
First of all it is not a query. it is a declaration of relation of the model to other models.
$hasAndBelongsToMany means that every record in your database is associated with many records of another table, and the many records from other table can be associated with the current record as well.
for example Book can have many authors, and an authors can have many books. so it can be related as hasAndBelongsToMany.
in your case Group has many users, and users have many groups. same for permissions.
Is there a shortcut for creating Unary associations in Cake?
For example, a user is a friend of another user. I'm pretty sure it's going to violate cake's conventions if I try it the hard way and code the associations myself.
Cake has HABTM:
var $hasAndBelongsToMany = array(
'User' =>
array(
'className' => 'User',
'joinTable' => 'friends',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
'unique' => true
)
);
Will this work? A user model assoc to another user model.
Update:
How do I save the data now?
$this->data["Friend"] = $request["Requester"];
$this->data["Admirer"] = $request["Requestee"];
$this->Friend->create();
$this->Friend->save($this->data);
$request["Requester"] and $request["Requestee"] both hold objects of type User.
The HABTM rel is defined in the User model
var $hasAndBelongsToMany = array(
'Friend' => array(
'className' => 'Friend',
'joinTable' => 'friends',
'foreignKey' => 'user_id',
'associationForeignKey' => 'id'
),
'Admirer'=>array(
'className'=>'Friend',
'joinTable'=>'friends',
'foreignKey' => 'friend_id',
'associationForeignKey' => 'id',
)
);
All that happens is the $data->["Friend"]'s id is stored in the id column of the friends table
Here is a solution I use in an example application
Two tables are needed, users and followers. The important fields are User->id, Follower->user_id, Follower->friend_id.
I hope this snippet helps.
<?php
class User extends AppModel {
public $useTable='users';
public $hasAndBelongsToMany=array(
'Friend'=>array(
'className'=>'User',
'joinTable'=>'followers',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
),
'Admirer'=>array(
'className'=>'User',
'joinTable'=>'followers',
'associationForeignKey' => 'friend_id',
'foreignKey' => 'user_id',
),
);
}
-teh
Bjorn,
he is asking about User habtm User...
your join table should be users_users and the relation should look like this
var $hasAndBelongsToMany = array(
'Friend' =>
array(
'className' => 'UserUser',
'joinTable' => 'users_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'friend_id',
'unique' => true
)
);
i think that should do the trick
In CakePHP there are several variables in your model available to do that, like $hasMany, $belongsTo and $hasAndBelongsToMany. Please read the Cookbook for further explaination.. http://book.cakephp.org/view/78/Associations-Linking-Models-Together .
In your example, I think you need HABTM.