CakePHP condition contain multi level model - cakephp-2.0

I want to write a condition for my second level PersonneBien where should I write the condition
$this->Reservation->find('all',array('conditions' => array('Reservation.idBien' => 9),'contain' => array('Bien'=>array('BiensPersonne'))
when I write debug
array(
(int) 0 => array(
'Reservation' => array(
'idReser' => '1',
'dateReserDu' => '2016-04-23',
'dateReserAu' => '2016-04-27',
'montantAPaye' => '500',
'etatReservation' => 'Reserve',
'iddev' => '0',
'idBien' => '9',
'id' => '24'
),
'Bien' => array(
'idBien' => '9',
'designBien' => 'Petit coeur a TUNIS ville',
'aPropos' => 'De Host � Milan tres apprecie, avec environ 700 re positifs me trouver en Tunisie quelques jours par mois. Je lai choisi, ici aussi, d'avoir ma maison ou trouver ma souffrance et mes souvenirs. Les couleurs et les objets mediterraneens trouve et rec',
'capaciteAccueil' => '3',
'nbSalleBain' => '1',
'nbChambre' => '2',
'nbLits' => '3',
'description' => 'De Host a Milan beaucoup apprecie, avec environ 700 commentaires positifs
'adresse' => 'rue ladikiya',
'immeuble' => '',
'affichable' => false,
'affichablePageAccueil' => false,
'idtypelogement' => '1',
'idVille' => '14',
'idPays' => '1',
'idTypeBien' => '2',
'posLat' => '36.8065',
'posLong' => '10.1815',
'BiensPersonne' => array(
[maximum depth reached]
)
)
)
)
Where should I write the condition?

Inside the Contain array you can add conditions to second level association like follows
$this->Reservation->find('all',
array(
'conditions' =>array('Reservation.idBien' => 9),
'contain' => array('Bein'=>array('conditions'=>array('BiensPersonne'=>'condition_value')))
);

Related

CakePHP 2.x retriving data with i18n in a loop

I'm looping trough Features (belongTo FeatureType) and find() FeatureType.name for each Feature.feature_type_id.
I'm getting first record correct (with i18n translation) but in all the rest the i18n translation is not in place, but given as separate record.
What I am doing wrong?
this is the debug of my result table:
array(
'FeatureType' => array(
'id' => '28',
'name' => 'kolor suwaka',
'comment' => 'kolor suwaka etui notebook',
'locale' => 'pol'
)
)
array(
'FeatureType' => array(
'id' => '7',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor materiału',
'FeatureType__i18n_comment' => 'gra w klasy'
)
)
array(
'FeatureType' => array(
'id' => '11',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor',
'FeatureType__i18n_comment' => 'kółko i krzyżyk (jasnoszare i ciemnoszare)'
)
)
array(
'FeatureType' => array(
'id' => '27',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'obwód głowy',
'FeatureType__i18n_comment' => 'rozmiar kapelusza czarownicy'
)
)
This is the code:
$features_str = "";
if (!empty($product['Features'])) {
foreach ($product['Features'] as $featureIndex => $feature) {
$featureTypeModel = new FeatureType();
$feature_type = $featureTypeModel->find("first", array("conditions" => array("FeatureType.id" => $feature['feature_type_id'])));
if (strlen($features_str) > 0) $features_str .= ", ";
$features_str .= $feature_type['FeatureType']['name'] . ': ' . $feature['name'];
unset($featureTypeModel);
}
}

CakePHP find remove fields or recursive find

I'm trying to slim down my finds queries, by specifying which fields to use. I don't understand how I can specify specific fields for my HABTM fields.
This is my find:
$lastviewed = $this->Lastviewedproduct->find('all', array(
'fields' => array(
'Lastviewedproduct.id', 'Lastviewedproduct.sessionid', 'Lastviewedproduct.product_id',
'Product.id', 'Product.name', 'Product.slug', 'Product.price',
//'Category.id',
//'Mediafile.*',
),
'conditions' => array(
'Lastviewedproduct.status' => 'active',
'Lastviewedproduct.sessionid' => $this->Session->read('Companyname.sessionid'),
'Product.hidden_on_site' => 0,
'Product.visibility' => 1,
'Product.status' => 'active',
),
'order' => 'Lastviewedproduct.modified DESC',
'limit' => 5,
'recursive' => 2,
));
This is what it returns:
(int) 2 => array(
'Lastviewedproduct' => array(
'id' => '97',
'sessionid' => '14035312318244955',
'product_id' => '2'
),
'Product' => array(
'id' => '2',
'name' => 'blokhut 2',
'slug' => 'blokhut-2',
'price' => '200.00',
'Category' => array(
(int) 0 => array(
'id' => '218',
'zosomodule_id' => '25',
'user_add_id' => '0',
'user_update_id' => '0',
'created' => '2013-11-27 10:00:00',
'modified' => '2013-11-27 10:00:00',
'status' => 'active',
'visibility' => true,
'parent_id' => '2',
'shipment_id' => '1',
'name' => 'Metalen Tuinbergingen ',
'slug' => 'metalen-tuinbergingen-',
'pagetitle' => 'Metalen Tuinbergingen ',
'content' => '<p class="p1"><span class="s1">Metalen tuinbergingen zijn een praktische en kwalitatieve oplossing voor meer bergruimte in uw tuin. Wij hebben diverse merken metalen bergingen zoals </span><span class="s2">Biohort</span><span class="s1"> en </span><span class="s2">Yardmaster</span><span class="s1">. Hier hebben wij tevens ook tuinkasten en opbergboxen van.</span></p>
<p class="p1"><span class="s1"><br /></span></p>
<p class="p1"><span class="s1">Mocht u op zoek zijn naar andere varianten tuinbergingen, bekijk dan ons gehele assortiment </span><span class="s2">tuinbergingen.</span></p>',
'metatitle' => 'Metalen tuinbergingen',
'metadescription' => null,
'discountoffer' => false,
'discount_normal' => '0.00',
'discount_max' => '0.00',
'discount_ideal' => '0.00',
'cost_rembours' => '0.00',
'in_mainmenu' => true,
'hide_sidebar' => false,
'show_block' => true,
'sort_order' => '0',
'ProductsCategory' => array(
'id' => '2',
'created' => '2014-01-01 10:00:00',
'modified' => '2014-01-01 10:00:00',
'product_id' => '2',
'category_id' => '218'
)
)
),
'Mediafile' => array()
)
),
I don't want all these Category fields, but only Category.id, Category.name and Category.slug for example. How do i say that in my find? As you can see I tried adding Category.id in my fields array, but then I get an error. I read something about containable, but I didn't get that working and I don't know if that is the solution.
Thanks in advance for your help!
You can use ContainableBehavior for this task. You should write something like this:
1 attach behavior
permanent
class Lastviewedproduct extends AppModel {
public $actsAs = array('Containable');
....
}
on demand
$this->Lastviewedproduct->Behaviors->load('Containable');
2 specify fields
$this->Lastviewedproduct->find('all', array(
'contain' => array(
'Product' => array(
'Category' => array(
'fields' => array('id','name','slug')
),
),
'Mediafile'
)
));
Add field parameter with relation ship where you give relationship to Category MODEL
public $hasMany = array(
'Category' => array(
'className' => 'Category',
'fields' => array('Category.id','Category.name','Category.slug')
)
);
As Dimitry said above - Containable is the way to go with this:
In your Models/AppModel.php (Create it if it doesn't exist) change the call to have the following:
class AppModel extends Model {
public $actsAs = array('Containable');
}
Then you would do as Dimitry said above - this will allow you to only return the fields you want.
'recursive' => 2
will eventually cause you so much slowness that your app will be painful to use, most people set recursive = 1 in their AppModel and use containable the rest of the time.

Accessing the array values in cakephp

I need to access the array elements, from the below array structure.Here i need to access the NewsFeedComment array.I am trying to do this, but it's giving me the error Undefined index: comments
How can i access the elements in the NewsFeedComment array
In Controller:
$this->set('newsfeed',$this->paginate('NewsFeed',$newsfeed));
In view:
<?php foreach ($newsfeeds $newsfeed): ?>
<div class="news_reply_display">
<?php echo $newsfeed['NewsFeedComment'][0]['comments'] ?>
</div>
<?php endforeach; ?>
array(
(int) 0 => array(
'NewsFeed' => array(
'id' => '2',
'title' => 'Best Restaurant',
'group_id' => '1',
'posted_message' => 'message.',
'user_id' => '11',
'created' => '2013-12-03 09:35:02',
'status' => 'A'
),
'Group' => array(
'id' => '1',
'group_name' => 'My group',
'created_user' => '1',
'created_date' => '2013-04-24 15:31:09',
'privacy' => '0',
'membership' => '0',
'description' => 'desc'
),
'NewsFeedComment' => array(
(int) 0 => array(
'id' => '1',
'news_feed_id' => '2',
'comments' => 'comment1',
'status' => 'A'
),
(int) 1 => array(
'id' => '2',
'news_feed_id' => '2',
'comments' => 'comment2',
'status' => 'A'
),
(int) 2 => array(
'id' => '3',
'news_feed_id' => '2',
'comments' => 'comment3',
'status' => 'A'
)
)
),
(int) 1 => array(
'NewsFeed' => array(
'id' => '1',
'title' => 'Test Subject',
'group_id' => '1',
'posted_message' => 'description.',
'user_id' => '1',
'created' => '2013-12-03 08:25:12',
'status' => 'A'
),
'Group' => array(
'id' => '1',
'group_name' => 'secondgroup',
'title' => 'Second Grouups',
'group_slug' => 'secgroup',
'created_user' => '1',
'created_date' => '2013-04-24 15:31:09',
'privacy' => '0',
'membership' => '0',
'description' => 'desc'
),
'NewsFeedComment' => array()
)
)
That index doesn't exist. In that particular case you must use $newsfeed[0]['NewsFeedComment']['comments'].
Now, if you would like to extract all "NewsFeedComent" arrays from $newsfeed then you can use Hash::extract
$newsFeedComments = Hash::extract($users, '{n}.NewsFeedComment');
debug($newsFeedComments);
foreach($newsfeeds as $newsfeed){
foreach($newsfeed['NewsFeedComment'] as $NewsFeedCommentData){
echo $NewsFeedCommentData['comments'];
}
}
Using the above code you will get comments corresponding to each index of array $newsfeeds

CakePHP 2.1 Avg function for query

I am using cakephp 2.1 and I wanna get average of movie ratings for perticular movie.
Where 'Movie' is a model and it contains 'Language' and 'CriticReview' as models.
The array structure is as follows.
array(
(int) 0 => array(
'Movie' => array(
'id' => '4',
'name' => 'Maattrraan',
'cover_image' => '/movies/4d0d1c41d956a2cb2ab93603d1fdf70c.jpg',
'release' => '2012-10-12',
'runtime' => '',
'budget' => '65 crore',
'box_office' => '',
'language_id' => '2',
'industry_id' => '3'
),
'Language' => array(
'id' => '2',
'name' => 'tamil'
),
'CriticReview' => array(
(int) 0 => array(
'rating' => '4',
'movie_id' => '4'
),
(int) 1 => array(
'rating' => '3',
'movie_id' => '4'
)
)
),
(int) 1 => array(
'Movie' => array(
'id' => '1',
'name' => 'Romeo',
'cover_image' => '/movies/32aa2788fa5e1584d4c627c56214574e.jpg',
'release' => '2012-07-06',
'runtime' => '',
'budget' => '',
'box_office' => '',
'language_id' => '1',
'industry_id' => '1'
),
'Language' => array(
'id' => '1',
'name' => 'kannada'
),
'CriticReview' => array(
(int) 0 => array(
'rating' => '6',
'movie_id' => '1'
),
(int) 1 => array(
'rating' => '3',
'movie_id' => '1'
)
)
));
So I have to avg of critic review. Please help me to find out the solution. The work will be appreciable.
I don't know how your query looks like now, but you can try to add this field:
$this->Movie->find("all", array(
"fields" => array("AVG(CriticReview.rating) AS AverageRating"),
"conditions" => ...
));
The array returned will now contain a sub array within each movie with a key 0 like this
[0] => Array
(
[AverageRating] => AVG_CALCULATED_BY_MYSQL
)
I personally prefer to save calculated data instead of calculate it on fly. Maybe you'd like to read this question: MySQL - Calculating fields on the fly vs storing calculated data

Cake PHP joining same model on two tables

I have a question regarding model association. Consider the following application: each month a new question is asked to the users. These have the ability to give a tip on this question and other users have the ability to react on this tip. My model-setup is like this:
Users hasMany Tips, Reactions
Tips belongsTo User
Tips hasMany Reactions
Reactions belongsTo User, Tip
Now I want to get all the tips on that question from all users. I do a simple find all on the tips-table which return me all tips joined with their user and reactions. The reactions however do not contain the user, which is very important, obviously.
Cake automatically converts my find all to two queries: one on the tips-table joining the user and one on the reactions table, but there is no join on the users-table, which is what i need.
So my question is: why is the users-table not joined with the reactions and how can i solve this?
Code:
User-model:
public $hasMany = array('Tip', 'Reaction');
Tip-model:
public $hasMany = array('Reaction' => array(
'foreignKey' => 'tip_id'
));
public $hasOne = array('User' => array(
'fields' => array(
'User.id, User.first_name, User.last_name'
),
'foreignKey' => 'id'
));
Reaction-model:
public $belongsTo = array('Tip', 'ReactionUser' => array(
'className' => 'User',
'foreignKey' => 'id'
));
Controller:
$tips = $this->Tip->find('all');
OUTPUT:
array(
(int) 0 => array(
'Tip' => array(
'id' => '1',
'user_id' => '1',
'question_id' => '1',
'tip' => 'Testing da new datavbase',
'votes' => '0',
'approved' => '1',
'pic' => '',
'created' => '2012-05-18 09:22:56'
),
'User' => array(
'id' => '1',
'first_name' => 'Bart',
'last_name' => 'De Bock'
),
'Reaction' => array(
(int) 0 => array(
'id' => '1',
'tip_id' => '1',
'user_id' => '1',
'reaction' => 'lorem ipsum',
'votes' => '0',
'approved' => '1',
'pic' => '',
'created' => '0000-00-00 00:00:00'
)
)
),
(int) 1 => array(
'Tip' => array(
'id' => '2',
'user_id' => '2',
'question_id' => '1',
'tip' => 'Test',
'votes' => '0',
'approved' => '1',
'pic' => '',
'created' => '2012-05-18 10:22:51'
),
'User' => array(
'id' => '2',
'first_name' => 'Deborah',
'last_name' => 'Rochtus'
),
'Reaction' => array(
(int) 0 => array(
'id' => '2',
'tip_id' => '2',
'user_id' => '3',
'reaction' => 'Say whaat?',
'votes' => '2',
'approved' => '1',
'pic' => '',
'created' => '2012-05-18 10:33:32'
),
(int) 1 => array(
'id' => '3',
'tip_id' => '2',
'user_id' => '4',
'reaction' => 'Hmmm..',
'votes' => '0',
'approved' => '1',
'pic' => '',
'created' => '2012-05-18 10:33:44'
)
)
)
)
As you can see, these reactions do not contain the associated user...
Thanks for your help!
Use Containable:
In AppModel:
public $uses = array('Containable');
Change your find call to:
$this->Tip->find('all', array('contain' => array('User', 'Reaction' => array('User'))));
Please try this
$tips = $this->Tip->find('all'
array(
'recursive'=>'2'
)
);
recursive value maybe 1, 2, 3, 4 etc as you need.

Resources