paginating habtm relations - cakephp

got this situation. Reports habtm users. So Im trying to paginate just the Reports that are linked to the Auth user... I read when you have a habtm relationship you have to bind the model temporarily using 'hasOne' like this:
function index(){
$conditions=array('ReportsUser.user_id'=>$this->Auth->User('id'), 'ReportsUser.report_id'=>'Report.id');
$this->beforeFind();
$this->Report->recursive=0;
$this->set('reports',$this->paginate($conditions));
}
function beforeFind()
{
$this->Report->bindModel('hasOne'=>array('ReportsUser'), false);
}
so here is the issue... that doesn't work...
that give me no results... I already checked the database for user having any Report, and i logged in with one of those user...
any suggestions?

GOT IT!!
$conditions=array('ReportsUser.user_id'=>$this->Auth->User('id'), 'ReportsUser.report_id'=>'Report.id');
i just had to remove 'ReportsUser.report_id'=>'Report.id' cuz cake was searching for it for a second time... so i just left
$conditions=array('ReportsUser.user_id'=>$this->Auth->User('id'));
and i add
$this->Report->bindModel('hasOne'=>array('ReportsUser'**=>array('className'=>'ReportsUser', 'foreignKey'=>'report_id')**), false);

Related

CakePHP3 associated entities not coming through from find() (or get) even with contain clause

[edit] solved, see: https://stackoverflow.com/a/32638610/221650
Some associated data is not coming through, although it's in the contain clause.
I can get $project->Participants if I set a belongsToMany relationship in the Projects table, relating to Participants through ProjectParticipants, but that way I still can't reach other tables associated with ProjectParticipants even with $project->participants->_joinData->other_related_table
How would I do to get ProjectParticipants and Participants with the same query?
Code:
// Database: Projects <--1:N-- ProjectParticipants --M:1--> Participants
// ProjectController:
$project = $this->Projects->get($id, ['contain'=>[
'ProjectParticipants.Participants']);
// ProjectsTable:
$this->hasMany('ProjectParticipants', [
'foreignKey' => 'project_id']);
// ProjectParticipantsTable:
$this->belongsTo('Participants', [
'foreignKey' => 'participant_id']);
It's a very complicated many to many relationship.
//projectTable
$this->belongsToMany('Participants');
//praticipantsTable
$this->belongsToMany('Projects');
It's well explained in the Bookmark Tutorial.
Sorry, it's working fine, I hadn't noticed there was a custom Entity\Project::_getParticipants() that was returning a Collection.
A simple debug on that object showed everything alright, but then when running it through a foreach, the associations disappeared.

Can't populate a Select options with related model info - CakePHP

Puling my hair out with this, I know its something very simple but I've been struggling to resolve it. For the record I'm using cakePHP 2.2.
I'm trying to populate a select input with related model data.
Basically I have users and user_statuses. The user model belongsTo the UserStatus model. Within the user/add() function I want to have a drop down select box populated with the data from the UserStatus model.
Heres the code:
UsersController/Add()
$groups = $this->User->Group->find('list');
$UserStatus = $this->User->UserStatus->find('list');
$this->set(compact('groups', 'UserStatus'));
View/Users/add.ctp
echo $this->Form->input('user_status_id', array('class' => 'span9'));
The field in the users table making the relationship is user_status_id which links to the id in the user_statuses table.
Based on the above the select box will NOT populate with the statuses from the user_statuses table. Interestingly enough, it will if I change the view code to just :
echo $this->Form->input('user_status_id', array('class' => 'span9'));
However, when I do this is will NOT write to the database.
Any help will be gratefully received.
Thanks in advance.
The options param should solve the problems for display and as long as there is a user_status_id in your User table, the below code should save the value in your User model on save.
echo $this->Form->input('User.user_status_id', array('options'=>$UserStatus,'class' => 'span9'));
Should be:
$userStatus = $this->User->UserStatus->find('list');
(note the lowercase 'u')

CakePHP $this->Auth->user('id') returns incorrect user in controller

I've got a really bizarre bug occurring in a CakePHP app I've been working on.
I've got the following method in my users_controller.php that reads the currently signed in user and sends the data to a view and sets the title_for_layout to the user's name:-
function account() {
$this->User->id = $this->Auth->user('id');
$this->set('User', $this->User->read());
$this->set('title_for_layout', 'Welcome '.$this->User->Contact->field('name'));
}
In my view I've got (among other things):-
<?php echo $User['Contact']['name'] ?>
Everything in the view looks fine. It is outputting the fields for the correct user (the one I am currently signed in as). However the title_for_layout is using a completely different user's details. So $this->User->Contact->field('name') is not the same as $User['Contact']['name']!
I can't spot what is going wrong here so hoping someone out there can point out my mistake.
Model read() simply does a find('first'), sets the result as the Model $data, and returns it. It doesn't set $ids of associated models.
So in your case, $this->User->id is set, but $this->User->Contact->id isn't.

validateAssociated overwriting associated model's errors, possible bug?

I'm running with cakephp version 2.0.2 and was scratching my head as to why a form submission that submits data to an association of models was not revealing error messages for the associations.
I've been digging into the Model class to diagnose further. I found that if the primary model for the form had its own validation errors, then no validation errors for any associations would ever be revealed in the returned:
$this->validationErrors
But I think I found the smoking gun. In the Model.php's validateAssociated method, you'll see this:
$this->validationErrors = $validationErrors;
if (isset($validationErrors[$this->alias])) {
$this->validationErrors = $validationErrors[$this->alias];
}
The first line sets $this->validationErrors to contain all built up errors across all associations. But if $validationErrors contains errors for the key of $this->alias which is the primary model name, then as you can see, $this->validationErrors gets overwritten to just those errors.
So this begs the question.... why? I'm so certain this is a bug I want to modify my Model.php and I think it'll work. But I wanted to get this in front of others in case I'm doing something really stupid here.
I had the same issue today. Its like that for BC. I know, it sucks. It should be a bug. The way I work around it is by re-formatting validation errors.
// AppModel.php
public function formatValidationErrors($models) {
foreach($models as $model => $assoc) {
if (is_numeric($model)) {
$model = $assoc;
$assoc = null;
}
$this->validationErrors[$model] = $this->{$model}->validationErrors;
if ($assoc) {
$this->{$model}->formatValidationErrors($assoc);
}
}
}
I call that if validation fails, and pass an array like you would do to contain. You can use that if you don't want to modify the core.

cakephp association error?

Warning (512): Model "User" is not associated with model "User" [CORE\cake\libs\model\behaviors\containable.php, line 340]
im getting this error when accessing datas of photo, friend
user has many photos and friend, photos and friend belongs to user
in photos index page, two warnings one for user mentioned above and other for 'Friend' as same friend is not associated with model 'friend'
what to do? what to check?
You have some mismatch with assotiations.
You can have situation similar to this:
User habtm array("Friend"=> array("className"=>"User")
And when finding users:
$this->User->find("all", array(
"contain"=>array("User");
));
instead of:
$this->User->find("all", array(
"contain"=>array("Friend");
));
Check this or post some code :)

Resources