Cakephp - Multiple users with different profile fields - php

I have three different type of users with different columns for profiles.
I currently have Users table and Groups table with Users table linking to Groups.
How can I set it so that it picks up different type of profiles for each group users?

you can use $belongsTo relation for linking for both table, such that every user belongs to some group.
For that you need to create model of user. Try something like following code
class Users extends AppModel {
var $name = 'Users';
public $belongsTo = array(
'Groups' => array(
'className' => 'Groups',
'foreignKey' => 'group_id'
)
);
}

Related

CakePHP Associations - hasMany but need to match column in 3rd table?

I am new to CakePHP (2.x) and am creating a post and comments feature. Everything works except I cannot figure out how to get the user's username out of the registrations (third) table (linked with "registration_id"). My associations currently look like:
class Article extends AppModel {
public $hasMany = array('ArticleComment');
public $belongsTo = array(
'ArticleRegistration' => array(
'className' => 'Registration',
'foreignKey' => 'Article.registration_id' //(doesn't work)
),
'ArticleCommentRegistration' => array(
'className' => 'Registration',
'foreignKey' => 'ArticleComment.registration_id' //(doesn't work)
)
);
class ArticleComment extends AppModel {
public $belongsTo = array('Registration','Article');
I am not sure if the associations from ArticleComment are being applied since it is being called through the Article model. I am retrieving the data by:
$this->set('articles', $this->Article->find('all', array('order' => 'Article.created desc', 'limit' => '3')));
I have tried a join and passing two separate variables for the articles and comments array but then I have to remove my associations which leads me to believe it's not proper coding.
Tables are:
articles
__________
id
registration_id
body
article_comments
__________
id
article_id
registration_id
body
registration
__________
id
username
I am fetching the information with:
$this->set('articles', $this->Article->find('all', array('order' => 'Article.created desc')));
TIA!
The generated SQL query from cakephp when you make a find() call is as follows
SELECT `Article`.`id`,
`Article`.`registration_id`,
`Article`.`body`,
`Registration`.`id`,
`Registration`.`username`
FROM `test`.`articles` AS `Article`
LEFT JOIN `test`.`registration` AS `Registration`
ON (`Article`.`registration_id` = `Registration`.`id`)
WHERE 1 = 1 LIMIT 20
So, in order to access the username you just need to write something like this in your view
$articles['Registration']['username'];
I recommend you to use the cakephp bake commands, I could make this proyect like in 10 minutes with them. And also I recommend using Netbeans as IDE, the cakephp plugin is awesome.
Here is a link to an example project that you can clone with git.

cakephp - getting joins correct

I'm a bit of a newbie and I'm having trouble getting my cakephp query correct.
I have a users table and users hasmany rotas. The rotas table looks like
monday_am
monday_pm
tuesday_am
etc
and if the user is due to be in on monday_pm for example, then that field will have 1. Otherwise 0.
I want to get all the users who belong to a particular room, have deregistered=0 and - and this is where I'm having the problem - who have a rota where (for example) tuesday_am = 1
So I have the code:
$options['conditions'] = array('User.room_id'=>$room_id, 'User.deregistered'=>0, 'User.request'=>0);
$options['joins'] = array(
array('table' => 'rotas',
'alias' => 'rota',
'type' => 'INNER',
'conditions' => array('rota.'.$todaysDay.'_'.$amPm => 1)
)
);
$usersToday = $this->User->find('all', $options);
If I don't try the join then I get a nice small list of users like I expect. If I try the join I get 100s of results which are mostly duplicates!
How can I get just one record of a user who has the required conditions in the Users table as well as the Rotas table, and how come doing the join as I have causes so many results?!?
thanks
Assuming you have a column in your rota table called user_id, you can make your Rota model like this:
class Rota extends Model
{
public $belongsTo = array('User');
}
Then in your controller, you can query the Rotas and get the user from it.
$users = $this->Rota->find('all', array(
'fields' => array('DISTINCT (User.id)' /* add more fields for selection */ ),
'conditions' => array(
'User.room_id'=>$room_id,
'User.deregistered'=>0,
'User.request'=>0,
'Rota.'.$todaysDay.'_'.$amPm => 1
)
));

Getting All groups that belong to user

I have HABTM for Users and Groups. I want to show in a Group view - all the Groups that belong to a User. Or to put it differently - all the Groups that have the User.
I am getting tangled in the MVC and am not able to figure it out. Here are my two models:
class Course extends AppModel
public $name = 'Course';
public $hasAndBelongsToMany = array('User' =>
array(
'unique' => false
)
);
And...
public $name = 'User';
public $hasAndBelongsToMany = array('Course' =>
array(
'unique' => true
)
);
The table name in the database is courses_users - this table houses group ids and user ids.
Should be simple enough but I'm new to CakePHP so I'd love some help. Thank you!
CakePHP has recursive set to 1 by default, which means that assuming you have not changed the recursive setting, it will automatically fetch all associated courses when you call find on a user, assuming you set up the HABTM relationship when doing the find. Therefore, all you have to do is:
$this->User->find('first', array('conditions' => array('User.id' => $this->Auth->user('id'))));
In your User model, I don't think it's strictly necessary, but I like to specify the join table and such on HABTM relationships:
public $hasAndBelongsToMany = array('Course' =>
array(
'unique' => true,
'dependent' => false,
'joinTable' => 'courses_users',
'foreignKey' => 'user_id',
'associationForeignKey' => 'course_id',
)
);
Keep in mind that in HABTM relationships, you don't ever really touch the joinTable beyond specifying which table to use as the joinTable when setting up the relationship. CakePHP will automatically do the rest of the work.

Accessing data in a many to many and one to many relations

I dont know howto accessing data from a related table (with ActiveRecords) and use all those records as a dataSource for a CActiveDataProvider. The main idea is that a user must be able toCRUD` only the courses from his classes.
I have the following tables:
user (id, name, age, email...),
user_class (user_id, class_id),
class (id, name, description, ...),
course (id, class_id, name, description);
User and Class tables have a MANY TO MANY and Class and Course tables have a ONE TO MANY relation
I also defined the following relations:
User model relations:
'classes'=>array(
self::MANY_MANY,
'Class',
'user_class(user_id, class_id)',
),
'courses'=>array(
self::HAS_MANY,
'Course',
array('class_id','id'),
'through'=>'classes'
)
Class model relations:
'users' => array(
self::MANY_MANY,
'User',
'user_class(class_id, user_id)'
),
'courses' => array(
self::HAS_MANY,
'Class',
'class_id'
),
Course model relations:
'class' => array(
self::BELONGS_TO,
'Class',
'class_id'
)
In my CourseController , on actionManage() I have something like this:
public function actionManage() {
$currentUser = User::model()->with('classes', 'courses')->findByPk(Yii::app()->user->id);
$userCourses = $currentUser->classes->courses; //--> NO courses!!!
/* add users` courses to an CActiveDataProvider //--> how to do this?!
}
If you try to var_dump($currentUser->classes); you'll notice that it's an array.
$userCourses = $currentUser->classes[0]->courses; will work if there are classes associated with $currentUser
Since you're loading many things lazily (each "class" loads its' "courses" the first time they're requested) this could become slow down the road, but for now, run with it I guess.

How to Retrieve fields from linked Models

I have the following three database tables:
Products
########
id
title
artist_id
Arists
######
id
profile
person_id
People
######
id
first_name
last_name
In my Product model how do I create a method to return the product title along with the artist's first_name?
I have set up the following model associations:
Product belongs to Artist
Artist belongs to Person
Containable is definitely the way to go for filtering related records. Make sure to add $actsAs = array('Containable') into your model or app_model.
Then you can do things like:
$this->Product->find('all', array(
'contain' => array(
'Artist' => array(
'Person' => array(
'id',
'first_name'
)
)
)
));
Assuming you already set the relationships in these models, you just need to set it recursive:
$this->Product->recursive = 2;
print_r($this->Product->find('all'));

Categories