Access Foreign Tables CakePHP - php

I am sorry if the title is a bit confusing, I just don't know how this is properly called. I have a table structure like this for my CakePHP project.
users id, name, surname, userrecords
userrecords id, user_id, records_id
records id, description
I understand that to access the userrecords middle table in my users view I have to do something like
$user['userrecords']['id'];
How can I neatly access description in records table through a users view?

You didn't specify whether you're using CakePHP 2.x or 3.x, so I provided solutions for both.
The relationship you are referring to is called a "Has And Belongs To Many" relationship. Since both of your model are associated to a linking table (userrecords), you can freely associate as many records to as many users as you want.
First, I would consider renaming your 'userrecords' table to 'users_records' to play nicely with CakePHP.
First, define your relationship within your Users model:
// Using CakePHP 2.x:
class User extends AppModel {
public $actsAs = array('Containable'); // Instantiate Containable behavior
public $hasAndBelongsToMany = array(
'Record' =>
array(
'className' => 'Record',
'joinTable' => 'users_records',
'foreignKey' => 'user_id',
'associationForeignKey' => 'record_id'
)
);
}
// Using CakePHP 3.x:
class UsersTable extends Table
{
public function initialize (array $config)
{
$this->belongsToMany('Records', [
'joinTable' => 'users_records' // Defines our linking table
]);
}
}
Now, we must define our relationship within our Records model:
// Using CakePHP 2.x:
class Record extends AppModel {
public $actsAs = array('Containable'); // Instantiate Containable behavior
public $hasAndBelongsToMany = array(
'User' =>
array(
'className' => 'User',
'joinTable' => 'users_records',
'foreignKey' => 'record_id',
'associationForeignKey' => 'user_id'
)
);
}
// Using CakePHP 3.x:
class RecordsTable extends Table
{
public function initialize (array $config)
{
$this->belongsToMany('Users', [
'joinTable' => 'users_records' // Defines our linking table
]);
}
}
Now, we can access the associated records freely from each model using the ORM's contain method:
// Using CakePHP 2.x:
// Getting 'User' and associated 'Record' models in Controller:
$this->loadModel('User');
$this->User->find('all', array('contain' => 'Record'));
// Getting 'Record' and associated 'User' models in Controller:
$this->loadModel('Record');
$this->Record->find('all', array('contain' => 'User'));
// Using CakePHP 3.x:
// Getting 'User' and associated 'Record' models:
$users_table = TableRegistry::get('Users');
$users = $users_table->find()->contain('Records')->all();
// Getting 'Record' and associated 'User' models:
$records_table = TableRegistry::get('Records');
$records = $records_table->find()->contain('Users')->all();
Read the Cookbook, it will make your life a million times easier:
CakePHP 2.x Containable Behavior
CakePHP 2.x Has And Belongs To Many Relationship
CakePHP 3.x belongsToMany Relationship
CakePHP 3.x Retrieving Associated Data
CakePHP 3.x Eager Loading Associations

Read this,this may be helpful for you
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

Because your table structure as below :
User -> UserRecord -> Record
So that you can only get [Record] via [UserRecord]
You should set recursive property in find command.
Please refer for more information about recursive at this link : what is the meaning of recursive in cakephp?
I hope this answer doesn't misunderstand your question.

Related

Saving HasMany Associations Data in CakePHP 3.x

I am having two tables. My primary table is Students. And my secondary table is Exams. I am trying to save both the tables using hasMany and belongsToMany Association. But It is saving data in Student table only, not in Exams. Can any one help me to resolve this problem.
Students Model :
class StudentsTable extends Table {
public function initialize(array $config) {
$this->addBehavior('Timestamp');
parent::initialize($config);
$this->table('students');
$this->primaryKey(['id']);
$this->hasMany('Exams', [
'className' => 'Exams',
'foreignKey' => 'student_id',
'dependent'=>'true',
'cascadeCallbacks'=>'true']);
}
}
Exams Model :
class ExamsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('exams');
$this->primaryKey(['id']);
$this->belongsToMany('Students',[
'className'=>'Students',
'foreignKey' => 'subject_id',
'dependent'=>'true',
'cascadeCallbacks'=>'true']);
}
}
My school.ctp :
echo $this->Form->create();
echo $this->Form->input('name');
echo $this->Form->input('exams.subject', array(
'required'=>false,
'multiple' => 'checkbox',
'options' => array(
0 => 'Tamil',
1 => 'English',
2 => 'Maths')));
echo $this->Form->button(__('Save'));
echo $this->Form->end();
In my controller:
public function school() {
$this->loadModel('Students');
$this->loadModel('Exams');
$student = $this->Students->newEntity();
if ($this->request->is('post')) {
$this->request->data['exams']['subject'] =
implode(',',$this->request->data['exams']['subject']);
$student = $this->Students->patchEntity(
$student, $this->request->data, ['associated' => ['Exams']]
);
if ($this->Students->save($student)) {
$this->Flash->success(__('The user has been saved.'));
} else {
$this->Flash->error(__('Unable to add the user.'));
}
}
}
Patching BelongsToMany Associations
You need to make sure you are able to set exams. Set accessibleFields to allow you to patch associated data
$student = $this->Students->patchEntity(
$student, $this->request->data, [
'associated' => ['Exams'],
'accessibleFields' => ['exams' => true]
]
);
You can also do this with the $_accessible property in the entity.
I've never done hasMany to belongsToMany because i don't think it works that way (I mean no harm in my words.) But I'll try to explain. Your relationships should be both belongsToMany because exams will have many students and students will have many exams. So basically they're the same either way. What you need is another table to connect them which will be called students_exams or exams_students (i think its exams_students because E comes before S) because in cake if you name everything properly most of it happens automatically.
Assuming you know how patchEntity works, creating your $this->request->data properly will patch it automatically and save it in the correct table when you save it. If you have any more questions feel free to ask more. :)

Cakephp 2.1 rename ACL Table without ACL Model

I have trouble using ACL. In my CoreUsersController i make the following call:
if(!$this->Acl->check('User::'.$user['User']['id'], 'CoreUser', 'read')) {
die('PERM DENIED');
}
The ACL is included as component in my AppController. The Database-Tables looks like:
table "acos"
table "core_groups" (as aros)
table "aros_acos" as connection between core_groups and acos.
Here is the Error Thrown:
Error: Table aros for model Aro was not found in datasource default.
And my Code:
1. User-Model:
App::uses('AuthComponent', 'Controller/Component');
class CoreUser extends AppModel {
public $useDbConfig = 'default';
public $useTable = 'core_users';
public $hasAndBelongsToMany = array(
'MemberOf' => array(
'className' => 'CoreGroup',
'joinTable' => 'core_users_groups',
'foreignKey' => 'user_id',
'associationForeignKey' => 'group_id'
)
);
public $name = 'User';
public $actsAs = array('Acl' => array('type' => 'both'));
public function parentNode() {
// stuff in here should be correct
}
}
2. Group-Model:
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class CoreGroup extends AppModel {
public $useDbConfig = 'default';
public $useTable = 'core_groups';
public $hasAndBelongsToMany = array(
'Member' => array(
'className' => 'CoreUser',
'joinTable' => 'core_users_groups',
'foreignKey' => 'group_id',
'associationForeignKey' => 'user_id'
)
);
public $name = 'Group';
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
}
My Question: How to tell ACL to use "core_groups"-Table instead of "aros"-table? Note: I added "ActsAs" as suggested by the Cake-Book to both Models!
If you would like to proceed with Cake's ACL, then it is better to have three separate tables:
1. aros
2. acos
3. aros_acos
These can be generated using the AclShell:
./Console/cake schema create DbAcl
Using one of your app's model as an ARO or ACO is not advised as ARO and ACO models are implemented as a tree structure. These should be separate from the models in your app that 'actsAs' an ACO / ARO.
When you do attach the Acl behavior in the $actsAs property to your User/Group models, the Acl behavior's callbacks kick in. These use the parentNode() methods defined in your User/Group models to create an ARO reference to the rows created in your User/Group models. These references are stored in the ARO table separately, pointing to the actual User and Group. Hence, the ARO table is necessary.
I would recommend you go through the ACL tutorial in the book as your implementation will be quite similar. Just be careful about using the HABTM relationship between User and Group, as that will need a bit of tweaking.

CakePHP return hasOne relationships within main result array

I have a model class in CakePHP defined like this:
class Programme extends AppModel {
public $hasOne = array(
'ProgrammeLikes' => array(
'className' => 'ProgrammeLikes',
'fields' => array('likes'));
}
When retrieving my models from the database they are returned as an array with an array keyed to 'Programme' and a separate array keyed to 'ProgrammeLikes' (which contains the 'likes' value correctly). In order to reduce the changes necessary to existing code I want the 'likes' value to be within the 'Programme' array.
Is this possible?
Thanks in advance
Use virtualFields here to get this thing to be done.
class Programme extends AppModel {
public $hasOne = array(
'ProgrammeLikes' => array(
'className' => 'ProgrammeLikes',
'fields' => array('likes')
);
public $virtualFields = array(
'likes' => 'SELECT likes FROM programme_likes AS ProgrammeLikes WHERE ProgrammeLikes.id = Programme.programme_likes_id'
);
// Where programme_likes_id is the foriegnkey for Programme model
}
Note: I assumed programme_likes is your table name for ProgrammeLikes Model and programme_likes_id is the foriegnkey for
Programme Model, so you can arrange the query in your own way that suits your requirement.

Retrieving data through two model relationships

I'm trying to retrieve some data through two model relationships with CakePHP. The models and their associations are as follows:
User hasOne Profile HABTM Skill
I would like the user's skills to be returned when I do a find() operation on the User model, and right now it isn't returned. Here's my find call which is being executed against the User model:
$this->paginate = array(
'conditions' => array(
'OR' => array(
'Profile.firstname LIKE' => "%$q%",
'Profile.lastname LIKE' => "%$q%"
)
)
);
It's returning user data and profile data, but not any skill data. I tried setting recursive to 2 or 3, but that doesn't help either. The only way I can get skill data is if I run find() against the Profile model, but I can't do that. For clarification here's the relevant models and their relationships:
// app/Model/User.php
class User extends AppModel {
public $hasOne = 'Profile';
}
// app/Model/Profile.php
class Profile extends AppModel {
public $belongsTo = 'User';
public $hasAndBelongsToMany = 'Skill';
// app/Model/Skill.php
class Skill extends AppModel {
public $hasAndBelongsToMany = 'Profile';
Can anyone help me get the users skills when retrieving user data? Thanks.
Use CakePHP's Containable Behavior. Your find will then look something like this:
$this->User->find('all',array(
'conditions' => array(
'OR' => array(
'Profile.firstname LIKE' => "%$q%",
'Profile.lastname LIKE' => "%$q%"
)
),
'contain' => array(
'Profile' => array(
'Skill'
)
)
));
MUCH simpler, easier to read, and voila - you get the data you want without needing to use the dreaded 'recursive'.

codeigniter doctrine many to many relationships

I have three tables.
User
id
Group
id
UserGroup
user_id
article_id
date_joined
Now I have three separate models to set up the relationship.
// User Model
<?php
class User extends Doctrine_Record
{
// define table columns in this function
public function setTableDefinition() {
}
// setup some options
public function setUp() {
$this->hasMany('Group as Groupss', array(
'local' => 'user_id',
'foreign' => 'group_id',
'refClass' => 'UserGroup'
));
// causes 'created_at' and 'updated_at' fields to be updated automatically
$this->actAs('Timestampable');
}
}
// Group Model
<?php
class Group extends Doctrine_Record
{
// define table columns in this function
public function setTableDefinition() {
}
$this->hasMany('User as Users', array(
'local' => 'group_id',
'foreign' => 'user_id',
'refClass' => 'UserGroup'
));
}
}
// UserGroups
<?php
class UserGroup extends Doctrine_Record
{
public function setTableDefinition()
{
$this->hasColumn('user_id', 'integer', 4, array(
'primary' => true
)
);
$this->hasColumn('achievement_id', 'integer', 4, array(
'primary' => true
)
);
$this->hasColumn('date_completed', 'timestamp', 25);
}
}
and now all i want to do is build the relationship inside my controller:
$user = Doctrine::getTable('User')->findOneByEmail('abcd123#gmail.com');
$group = Doctrine::getTable('Group')->findOneByID('1');
$user->Group = $user;
Both SQL commands are returning one result and when I run the controller I get the following error:
Fatal error: Uncaught exception 'Doctrine_Record_UnknownPropertyException' with message 'Unknown record property / related component "User" on "Group"' in
The refclass option in the User and Group classes should most likely be UserGroup and not UserGroups (Doctrine doesn't understand the difference between singular and plural). You should also invoke the parent::setUp() method in each of the setup() methods you declare.
There's an extensive amount of documentation on many-many relationships in the official manual. (Which uses the user-group relation as an example)
EDIT:
I noticed some severe problems with the code while going through it again:
Your UserGroup table has an article_id column, but no group_id column.
The declared relation to the Group model under the name Groupss, this making the relation $user->Group unknown.
Finally, you should assign the user to an array of groups like $user->Groups[] = $group

Categories