why cake php framework not recognize true syntax in this example - cakephp

why cake PHP framework not recognize true syntax in this example
User has Many Photos
Photos belongs to User
In User Model
var $hasMany = array( 'Photo' => array( 'className' => 'Photo', 'foreignKey' => 'user_id' );
In Photo Model
var $belongsTo = array( 'user' => array( 'className' => 'User', 'foreignKey' => 'user_id', 'conditions' => '', 'fields' => '', 'order' => '' ), 'PhotoAlbum' => array( 'className' => 'PhotoAlbum', 'foreignKey' => 'photo_album_id', 'conditions' => '', 'fields' => '', 'order' => '', ))
in Photo controller , in add action cake generate this code :
$this->Photo->User->find('all');
but true code is :
$this->Photo->user->find('all');
means user property should upper case but cake not recognize and bake false code.

Also - the problem isn't cake. The problem is you have a var $name = "user"; in your user model.
Name your model with the cake conventions correctly and this won't happen. When you don't follow conventions you run into problems with the framework. The correct solution is to read the manual instead of posting questions with near worthless debug info here # SO.

Try setting up the $uses var. In your case in the photos_controller:
$var uses = array('User');

i find that when happen my problem , this error happen when I use association in my class and i use uppercase name for user in my code of photo model's
var $belongsTo = array( 'user' => array( 'className' => 'User', 'foreignKey' => user_id', 'conditions' => '', 'fields' => '', 'order' => '' ), 'PhotoAlbum' => array( 'className' => 'PhotoAlbum', 'foreignKey' => 'photo_album_id', 'conditions' => '', 'fields' => '', 'order' => '', ))
but user is true with Under spelling (User)
var $belongsTo = array( 'User' ...

Related

CakeDc User $hasMany not returning Associated model

I am using the CakeDc Users plugin 2.0 and on the plugins admin_index view I would like to see the associated Books for the users.
I have added $hasMany to User.php Model
public $hasMany = array(
'Book' => array(
'className' => 'Book',
'foreignKey' => 'user_id',
'dependent' => true,
'conditions' => '',
'fields' => '',
'order' => 'order',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
And in the UserController.php I added 'contain'
protected function _setupAdminPagination() {
$this->Paginator->settings = array(
'limit' => 20,
'order' => array(
$this->modelClass . '.created' => 'desc'),
'contain' => array('Book')
);
}
public function admin_index() {
$this->Prg->commonProcess();
unset($this->{$this->modelClass}->validate['username']);
unset($this->{$this->modelClass}->validate['email']);
$this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;
if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) {
$parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
} else {
$parsedConditions = array();
}
$this->_setupAdminPagination();
$this->Paginator->settings[$this->modelClass]['conditions'] = $parsedConditions;
$this->set('usersList', $this->Paginator->paginate());
$this->layout = 'btst';
}
However I do not get the associated books in the view, I still just the array of users. This should work?
UPDATE:
debug ($this->Paginator->settings);
Output
array(
'limit' => (int) 20,
'order' => array(
'User.created' => 'desc'
),
'contain' => array(
(int) 0 => 'Book'
),
'User' => array(
'conditions' => array()
)
)
Debug $this->Paginator->settings before calling $this->Paginator and try
'contain' => array('Book')
However it is bad practice to modify plugins directly, you won't be able to update them anymore without trouble (merge conflicts, API changes...) in the worst case. The plugins documentation comes with a lot documentation that explains how to extend the plugin instead of modinfy it directly.
Edit: This was actually a bug in the plugin, fixed it in https://github.com/cakedc/users/commit/73aa350

CakePHP 2.4.2: Comments Model Relations-Get Original Post's Title

I have a pretty typical Comment model where Comment belongsto many other models such as Article, Review, Photo, etc and in turn each of those models hasmany Comment. Here is how I built the relationship on the Comment model...
<?php
App::uses('AppModel', 'Model');
class Comment extends AppModel {
var $name = "Comment";
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Article' => array(
'className' => 'Article',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Photo' => array(
'className' => 'Photo',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Review' => array(
'className' => 'Review',
'foreignKey' => 'post_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
This works like a charm wherein if I'm viewing a particular article then I can retrieve all comments from that article and it works the same for the other models. What I'm trying to do is show ALL recent comments with the original post's title no matter what model the original post is coming from (Article, Review, Photo, etc) in the format of $comment['Original']['title']. I thought adding the below code within the belongsto of the Comment model would work but it does not....
'Original' => array(
'className' => 'Article',
'foreignKey' => 'post_id',
'conditions' => array('Comment.module_id' => 3),
'fields' => '',
'order' => ''
),
'Original' => array(
'className' => 'Review',
'foreignKey' => 'post_id',
'conditions' => array('Comment.module_id' => 2),
'fields' => '',
'order' => ''
),
'Original' => array(
'className' => 'Photo',
'foreignKey' => 'post_id',
'conditions' => array('Comment.module_id' => 8),
'fields' => '',
'order' => ''
),
Unfortunately this only shows the correct title if the recent comment was on a photo (Comment.module_id = 8).
You can use CakePHP's Containable Behavior and do this:
//within a method of the Comment model
$recentComments = $this->find('all', array(
'contain' => array(
'Article',
'Photo',
'Review',
'User'
),
'limit' => 10,
'order' => $this->alias . '.created DESC'
));
Then, you'll get back the comments and any of their parents, regardless of which model it's in. After that, as you repeat through each comment, you can either do a case statement, or an if(!empty()) kinda thing to determine which model has content that you want to display.
(side note: not a good idea to create multiple associations with the same name per your attempt with "Original")
It's not surprising that you got comments only with conditions Comment.module_id = 8. You have made a basic PHP mistake. By using the same key Original multiple times you are effectively overwriting it's value each time. Choose unique aliases (array keys) for each association.

Cakephp 2.x find first entry of releated model

I know that i can easily find related model-data through model associations, if I select a single entry. But what I want to do is to get the first image as a thumb for the galery, when I use find('all') for the index-page.
My relations (Galleries are named Albums) are:
//Album-Model
public $hasMany = array(
'Picture' => array(
'className' => 'Picture',
'foreignKey' => 'album_id',
'dependent' => true,
'conditions' => ''
)
);
and:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Album' => array(
'className' => 'Album',
'foreignKey' => 'album_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
EDIT :
This should worked I tested it on my own!
Before your query add the following lane to specify the recursivity.
$this->Albums->recursive = 1;
$this->set('Albums', $this->Album->find('all'));
This way, it will also send the Pictures data. Then in your model associations, you can specify to only return the first image with the Album if that's what you want!
Just add 'limit' => '1', in your hasMany => Comments array, in Album Model.
Then you can iterate in your view doing
foreach($Albums as $Album){
$Album['Album']; // ALBUMS info do whatever
$Album['Picture']; // The thumbnail
}
EDIT2: I've just see you can also do
$this->Album->find('all', array('recursive' => 1));

Cake php default bahaviour for Translate behaviour not working

Can anyone suggest me why when there are no translation for a field it is returned empty, and not with the default translation?
I will appreciate all suggestions...
Can`t tell what part of my code you want to see, because it is all straight forward from www.book.cake.org. So i pasting some code:
Model definition:
<?php
class Article extends AppModel
{
var $useTable = 'nc_articles';
var $name = 'Article';
var $actsAs = array(
'Translate' => array(
'title', 'content', 'meta_key_words','meta_description'
)
);
// Use a different model
var $translateModel = 'ArticlesI18n';
// Use a different table for translateModel
var $translateTable = 'nc_articles_i18ns';
var $belongsTo = array(
'Author' => array('className' => 'User',
'foreignKey' => 'author_id',
'conditions' => '',
'fields' => array(),
'order' => '',
'counterCache' => ''),
'Modifier' => array('className' => 'User',
'foreignKey' => 'modifier_id',
'conditions' => '',
'fields' => array(),
'order' => '',
'counterCache' => ''),
'Category' => array('className' => 'ArticlesCategory',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => array(),
'order' => '',
'counterCache' => ''),
'Layout' => array('className' => 'Layout',
'foreignKey' => 'layout_id',
'conditions' => '',
'fields' => array(),
'order' => '',
'counterCache' => ''),
);
var $hasMany = array(
'Comments' => array(
'className' => 'ArticlesComment',
'foreignKey' => 'article_id',
'conditions' => array(),
'order' => '',
'limit' => '',
//'dependent'=> true When dependent is set to true, recursive model deletion is possible. In this example, Comment records will be deleted when their associated User record has been deleted.
)
);
}
?>
And then the function where I am changing Language:
function setLanguage($languageCode='pol')
{
$this->Session->write('Config.language', $languageCode);
$this->redirect($this->referer());
}
Would you like to see something more?
Let me answer my own question. There were few more things I had to write more. First of all I had to declare my Config.language in core.php then for Every model which is translated I had to declare a local variable which is an array of language codes
$this->Article->array('en','pol');
And that's how I managed the problem. But now queries for translation are very long and I am facing an optimization problem.

hasAndBelongsToMany problem by changing the condition in the model

I select Blog entries by changing hasAndBelongsToMany conditions on the fly, it doesnt work anymore for me with cakephp 1.3.
Strange problem cause it was working fine with 1.2, in the model i test it by putting a condition with a static id to see what happen, (Tag.name => 'libros'). but it pass though the hasAndBelongsToMany condition. Bring me whatever results.
var $hasAndBelongsToMany = array('Tag' =>
array('className' => 'Tag',
'joinTable' => 'blogs_tags',
'foreignKey' => 'blog_id',
'associationForeignKey'=> 'tag_id',
'conditions' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderSql' => '',
'deleteQuery'=> ''
)
in the controller
$this->Blog->bindModel(array(
'hasAndBelongsToMany' => array(
'Tag' => array('conditions'=>array('Tag.name'=>'libros'))
)));
$this->Blog->find('all');
Now i dont have mysql error anymore , but i have others records with others results. Weird.
If you have a correct database structure, must use this=
$this->Blog->bindModel(array(
'hasOne' => array(
'BlogsTag',
'FilterTag' => array(
'className' => 'Tag',
'foreignKey' => false,
'conditions' => array('FilterTag.id = BlogsTag.tag_id')
))));
$data = $this->Blog->find('all', array(
'fields' => array('Blog.*'),
'conditions'=>array('FilterTag.name'=>'libros')
));
you can read more about this at :
http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM

Resources