Second Level Expansion? - cakephp

First of all, sorry if this sounds too simple. I'm new to cakePHP by the way.
My objective is to expand the team_id in season controller where it linked thru standings table.
My current model rule are:
Each season has many standings
Each standings denotes a team.
Basically is a many to many relation between season and team = standings.
Here's my SeasonController:
class SeasonsController extends AppController
{
public $helpers = array('Html', 'Form');
public function view($id)
{
$this->Season->id = $id;
$this->set('season', $this->Season->read());
}
}
When I browse a single season thru /cakephp/seasons/view/1, the $season array shows:
Array
(
[Season] => Array
(
[id] => 1
[name] => English Premier League 2013/2014
[start] => 1350379014
[end] => 0
)
[Standing] => Array
(
[0] => Array
(
[id] => 1
[team_id] => 1
[win] => 1
[lose] => 0
[draw] => 1
[GF] => 5
[GA] => 4
[season_id] => 1
[rank] => 1
)
[1] => Array
(
[id] => 2
[team_id] => 2
[win] => 0
[lose] => 1
[draw] => 1
[GF] => 4
[GA] => 5
[season_id] => 1
[rank] => 2
)
)
)
The result only shows team_id, but how can I get further expansion for team_id?
I have put below in my Team's model, but still doesn't auto linked. Below are all my models:
class Team extends AppModel
{
public $name = 'Team';
public $hasMany = array(
'Standing' => array(
'className' => 'Standing'
));
}
class Season extends AppModel
{
public $name = 'Season';
public $hasMany = array(
'Standing' => array(
'className' => 'Standing'
));
}
class Standing extends AppModel
{
public $name = 'Standing';
public $belongsTo = array(
'Team' => array(
'className' => 'Team',
'foreignKey' => 'team_id',
),
'Season' => array(
'className' => 'Season',
'foreignKey' => 'season_id',
),
);
}

TLDR:
Use CakePHP's [Containable] behavior.
Explanation & Code example:
"Containable" lets you "contain" any associated data you'd like. Instead of "recursive", which blindly pulls entire levels of data (and can often cause memory errors), "Containable" allows you to specify EXACTLY what data you want to retrieve even down to the field(s) if you'd like.
Basically, you set your model to public $actsAs = array('Containable'); (I suggest doing that in your AppModel so Containable is available for all models). Then, you pass a "contain" array/nested array of the associated models you want to retrieve (see below example).
After reading about it and following the instructions, your code should look similar to this:
public function view($id = null)
{
if(empty($id)) $this->redirect('/');
$season = $this->Season->find('first', array(
'conditions' => array(
'Season.id' => $id
),
'contain' => array(
'Standing' => array(
'Team'
)
)
);
$this->set(compact('season'));
}
Once you get more comfortable with CakePHP, you should really move functions like this into the model instead of having them in the controller (search for "CakePHP Fat Models Skinny Controllers"), but for now, just get it working. :)

Use :
$this->Season->id = $id;
$this->Season->recursive = 2;
$this->set('season', $this->Season->read());

Related

link between model cake php

I try to use CakePhp but i don't find the solution. Here is my problem :
I have 2 models ant i try to link it.
The First is Player (which mysql table is players)
The second is Toto (which mysql table is totos)
I have a field un players called toto_id
And in my model Player i wrote :
<?php
class Player extends AppModel {
public $validate = array(
'name' => array(
'rule' => 'notEmpty'
)
);
public $hasOne = 'Toto';
}
And in my controller PlayersController
class PlayersController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$params = array(
'order' => array("Player.firstname")
);
$a = $this->Player->find('all', $params);
print_r($a);
$this->set('liste', $a);
}
}
And my print_r displays
Array
(
[0] => Array
(
[Player] => Array
(
[id] => 2
[name] => DESMAISON
[firstname] => Christophe
[is_intern] => 1
[score] => 663
[created] => 2014-01-05
[modified] => 0000-00-00 00:00:00
[toto_id] => 2
)
)
[1] => Array
(
[Player] => Array
(
[id] => 10
[name] => CHARLERY
[firstname] => Daphné
[is_intern] => 1
[score] => 572
[created] => 2014-01-04
[modified] => 0000-00-00 00:00:00
[toto_id] => 0
)
)
I don't understand why in my array i haven't a reference to Toto. Someone can help me ?
Thank you for your help
Ps : i use cakephp 2.4.4 PHP 5.4.7
I think this is BelongsTo Association .
Instead of HasOne association try to use BelongsTo,
class Invoice extends AppModel {
public $belongsTo = array(
'Toto' => array(
'className' => 'Toto',
'foreignKey' => 'toto_id'
),
);
if you want to retrieve data from database you have to set your recursivity and there is two ways first one :
$players = $this->Player->find('all',array('recursive'=>2));
// or
$this->Player->recursive = 2;
$player = $this->Player->find('all');

Cake php multimodel form post parameters

I'm a cakephp newbie, and I was ordered to use the 1.3 version.
I can't understand (and both guides and api docs don't tell it) how I could create an HABTM association in a POST request.
I'm trying to create a wine, that could be made of many vines. For example I'm creating a "soave" whine, that is made of "garganega" and "chardonnay" vines.
How should the POST params should be?
Given theses models
class Wine extends AppModel{
var $hasAndBelongsToMany = array(
'Vine' => array(
'className' => 'Vine',
'joinTable' => 'wine_vines',
'foreignKey' => 'wine_id',
'associationForeignKey' => 'vine_id',
'with' => 'WineVine',
),
);
}
class Vine extends AppModel{
var $hasAndBelongsToMany = array(
'Wine' => array(
'className' => 'Wine',
'joinTable' => 'wine_vines',
'foreignKey' => 'vine_id',
'associationForeignKey' => 'wine_id',
'with' => 'WineVine',
),
);
}
class WineVine extends AppModel{
var $name = "WineVine";
public $belongsTo = array("Wine", "Vine");
}
I tried a POST like this:
Array
(
[Wine] => Array
(
[denomination] => Soave DOP
[fantasy_name] =>
[kind] => White
)
[Vine] => Array
(
[0] => Array
(
[name] => garganega
)
[2] => Array
(
[name] => chardonnay
)
)
)
but it does not perform any inserts in vine table, only in wine.
Here's the log:
2 INSERT INTO `wines` (`denomination`, `fantasy_name`, `kind`, `modified`, `created`) VALUES ('', '', '', '2013-10-25 17:27:14', '2013-10-25 17:27:14') 1 55
3 SELECT LAST_INSERT_ID() AS insertID 1 1 1
4 SELECT `WineVine`.`vine_id` FROM `wine_vines` AS `WineVine` WHERE `WineVine`.`wine_id` = 2 0 0 1
5 SELECT `Vine`.`id`, `Vine`.`name`, `Vine`.`created`, `Vine`.`modified` FROM `vines` AS `Vine` WHERE 1 = 1 5 5 0
6 SELECT `Wine`.`id`, `Wine`.`denomination`, `Wine`.`fantasy_name`, `Wine`.`kind`, `Wine`.`created`, `Wine`.`modified`, `Wine`.`producer_id`, `WineVine`.`id`, `WineVine`.`wine_id`, `WineVine`.`vine_id`, `WineVine`.`created`, `WineVine`.`modified` FROM `wines` AS `Wine` JOIN `wine_vines` AS `WineVine` ON (`WineVine`.`vine_id` IN (1, 2, 3, 4, 5) AND `WineVine`.`wine_id` = `Wine`.`id`)
after saving the wine, try injecting its id into the data array
$this->data['Wine']['id'] = $this->Wine->id;
and then call an overloaded Model::saveAssociated() that will save all vines and update the join table by itself.
this overloaded method is described at:
http://bakery.cakephp.org/articles/ccadere/2013/04/19/save_habtm_data_in_a_single_simple_format
edit: sorry, that's for cake 2.x
i just realized 1.3 has no saveAssociated method
edit 2: but it does work in cake 1.3 if you change the last line of the saveAssociated method to
return parent::saveAll($data, $options);

Method find() not working in CakePHP

I'm trying to use find('list') method, but without success.
When I try to LIST the types of paying, just one table of database was showing correct, another is doing correct SQL but it's returning me null (even with the sql get rows).
If try to read ONE field of Tpagamento, it works
If I change the displayField of Tpagamento to 'id', it works, but the query only show the id.
Here is the scenario:
(Parcelamento and Tpagamento have the same databasefields)
View Method:
public function view($id = null) {
$this->Ordemservico->id = $id;
if (!$this->Ordemservico->exists()) {
$this->Session->setFlash('This Order does not exist..','flash_error');
$this->redirect(array('action' => 'index'));
}
//Listing all orders // THIS IS WORKING
$os = $this->Ordemservico->read(null, $id);
$this->set('os',$os);
//Listing the types of paying //THIS IS WORKING
$this->loadModel('Parcelamento');
$parcelamentos = $this->Parcelamento->find('list');
$this->set('parcelamentos',$parcelamentos);
$this->loadModel('Tpagamento'); // THIS IS NOT WORKING
$tpagamento = $this->Tpagamento->find('list');
$this->set('tpagamento ',$tpagamento );
//Read a specific type of paying, WORK!
$this->loadModel('Tpagamento'); // THIS IS WORKING
$tpagamentos = $this->Tpagamento->read(null,'5');
$this->set('tpagamentos',$tpagamentos);
}
OrdemServico Model:
class Ordemservico extends AppModel {
public $displayField = 'cliente_id';
public $belongsTo = array(
'Tpagamento' => array(
'className' => 'Tpagamento',
'foreignKey' => 'tpagamento_id',
),
'Parcelamento' => array(
'className' => 'Parcelamento',
'foreignKey' => 'parcelamento_id',
),
);
}
Tpagamento Model:
class Tpagamento extends AppModel {
public $useTable = 'tpagamentos';
public $displayField = 'nome';
public $hasMany = array(
'Ordemservico' => array(
'className' => 'Ordemservico',
'foreignKey' => 'tpagamento_id',
),
);
}
Parcelamento Model:
class Parcelamento extends AppModel {
public $displayField = 'nome';
public $hasMany = array(
'Ordemservico' => array(
'className' => 'Ordemservico',
'foreignKey' => 'parcelamento_id',
),
);
}
Generated SQL DUMP:
4 SELECT `Parcelamento`.`id`, `Parcelamento`.`nome` FROM `tereza`.`parcelamentos` AS `Parcelamento` WHERE 1 = 1 5 5 0
5 SELECT `Tpagamento`.`id`, `Tpagamento`.`nome` FROM `tereza`.`tpagamentos` AS `Tpagamento` WHERE 1 = 1 4 4 0
6 SELECT `Tpagamento`.`id`, `Tpagamento`.`nome`, `Tpagamento`.`created`, `Tpagamento`.`modified` FROM `tereza`.`tpagamentos` AS `Tpagamento` WHERE `Tpagamento`.`id` = 5 LIMIT 1 1 1 0
In the Config/database.php I setted: 'encoding' => 'utf8'
The reason to work In one table and not another is because in one table (tpagamentos) I have special characters and in another not (parcelamentos).
To show two fields in a list use the following syntax:
$this->Parcelamento->find('list', array('fields' => array('id', 'other_field_name')));

join belongsTo in find method

Score belongs to Player:
class Score extends AppModel {
public $name = 'Answer';
public $belongsTo = array('Player');
}
In PlayersController, I want to get player scores, with his details.
Question #1: How to include belongsTo model in find method result? (join it)
Question #2: How to get sum of all scores distance (Score.distance) which belongs to that player? (I mean SUM(Score.distance), group by Score.player_id)
Note for Q1: Because each player has a lot scores, I don't like to join scores in each find method I use in that controller. I want to get them in just 1 action)
Cakephp automagic not actually join tables as you can see in sql_dump in your layout at bottom.
You will see below queries.
SELECT `Player`.`id`, `Player`.`name`, `Player`.`created` FROM `players` AS `Player` WHERE `Player`.`id` = 10 GROUP BY `Player`.`id`
SELECT `Score`.`id`, `Score`.`player_id`, `Score`.`scores` FROM `scores` AS `Score` WHERE `Score`.`player_id` = (10)
Quite clear that its not actually join table so you need to do it following way.
And following will help you because i have already tested it.
In Player Controller you need to unbind score model first and then custom code to join score table as below.
In player model create one virtual field as below.
<?php
class Player extends AppModel
{
var $name = 'Player';
var $displayField = 'name';
var $virtualFields = array
(
'Distance' => 'SUM(Score.scores)'
);
var $hasMany = array
(
'Score' => array
(
'className' => 'Score',
'foreignKey' => 'player_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
?>
For testing i manually added conditions to find player number 10 scores
<?php
class PlayersController extends AppController {
var $name = 'Players';
function index()
{
$this->Player->unbindModel(array
(
'hasMany' => array
(
'Score'
)
));
$this->Player->bindModel(array
(
'belongsTo' => array
(
'Score' => array
(
'foreignKey' => false,
'conditions' => array
(
'Player.id = Score.player_id'
)
)
)
));
$order = "Player.id";
$direction = "asc";
if(isset($this->passedArgs['sort']) && $this->passedArgs['sort']=="Distance")
{
$order = $this->passedArgs['sort'];
$direction = $this->passedArgs['direction'];
unset($this->passedArgs['sort']);
unset($this->passedArgs['direction']);
}
$this->pagination = array();
$this->pagination = array
(
'conditions' => array
(
'Score.player_id' => 10
),
'fields' => array
(
'Player.*',
'SUM(Score.scores) AS Distance'
),
'group' => array
(
'Score.player_id'
),
'limit' => 15,
'order'=>$order." ".$direction
);
$playersScore = $this->paginate('Player');
}
}
?>
And the resulting array would be looks like below.
Array
(
[0] => Array
(
[Player] => Array
(
[id] => 10
[name] => Dexter Velasquez
[created] => 2012-08-02 12:03:07
[Distance] => 18
)
)
)
For testing i have used Generate Mysql Data.
for sorting link
<?php $direction = (isset($this->passedArgs['direction']) && isset($this->passedArgs['sort']) && $this->passedArgs['sort'] == "Distance" && $this->passedArgs['direction'] == "desc")?"asc":"desc";?>
and display above link as below.
<?php echo $this->Paginator->sort('Distance','Distance',array('direction'=>$direction));?>
So, Player has many Scores...
class Player extends AppModel {
public $name = 'Player';
public $hasMany = array('Score');
}
... which means your find() in PlayersController can be something like...
$players = $this->Player->find(
'all',
array(
'contain' => array('Score'), // <-- If you're not using Containable Behavior, remove.
'conditions' => array('Player.id' => $id), // <-- Change or remove as necessary.
'fields' => array(
'Player.id',
'Player.name',
'Sum(Score.distance) as sum_dist',
),
'group' => array(
'Player.id',
'Player.name',
),
'order' => array(
'Player.name',
),
)
);
Hopefully you get the idea.

Not sure how to retrieve my the values from array

I have two issues with my CakePHP application (its my first one in CakePHP). I am trying to convert an old php website to cake.
1.Issue
I have my controller that accepts a parameter $id, but the data is comming from joined tables so in the cookbook it had something like this
MY Controller
dish_categories_controller.php
class DishCategoriesController extends AppController {
var $uses = array("DishCategory");
var $hasOne ='';
function get_categories($id)
{
$this->set('dishes',$this->DishCategory->find());
$this->layout = 'master_layout';
}
}
model
dish_category.php
class DishCategory extends AppModel{
var $name = 'DishCategory';
var $hasOne = array(
'Dish' => array(
'className' => 'Dish',
'conditions' => array('Dish.id' => '1'),
'dependent' => true
)
);
}
As you can see the Dish.id=> '1' is hard coded, how can make it dynamic there so that I pass a value and make it something like Dish.if =>$id ?.
So that was my first issue.
The second issue is related to the view
That model returns only one record, how can I make it so that it returns all and also how would I be able to loop through that, below the code in the view currently and the array format.
This is in my view
<?php
echo $dishes['DishCategory']['category_info'];
echo $dishes['DishCategory']['category_title'];
echo $dishes['Dish']['dish_name'];
echo $dishes['Dish']['dish_image'];
echo $this->Html->image($dishes['Dish']['dish_image'], array('alt' => 'CakePHP'))
?>
Array Format
Array ( [DishCategory] => Array
( [id] => 1 [category_name] => Appetizers
[category_keywords] => appetizer, appetizers
[category_title] => Our Side Dishes
[category_info] => Test Test
[dish_id] => 1 )
[Dish] => Array ( [id] => 1
[dish_name] => Rice
[dish_disc] => The Best flavor ever
[dish_price] => 2.90 [dish_image] => /img/rice_chicken.jpeg [dish_category_id] => 1
[dish_price_label] => Delicious Arepa ) )
I would appreciate your help to help me understand how to better do this. Thank you.
Firstly, you DishCategoriesController has model properties, you can remove them. In your controller, you will set up the conditions for the find like so:
class DishCategoriesController extends AppController {
function get_categories($id)
{
// find category with a dish of $id
$this->set('dishes', $this->DishCategory->find('all', array(
'conditions' => array(
'Dish.id' => $id
)
)));
// set master layout
$this->layout = 'master_layout';
}
}
Your DishCategory model will look very basic, you don't need to hard code the relationship condition:
class DishCategory extends AppModel {
/**
* hasOne associations
*
* #var array
*/
public $hasOne = array(
'Dish' => array(
'className' => 'Dish',
'foreignKey' => 'dish_category_id'
)
)
}
At this point it is worth noting that since the DishCategory hasOne Dish, using the above find query, it will only ever return a single result. But, it you were returning multiple results, you could loop through them in your view like so:
<?php foreach ($dishes as $key => $dish): ?>
<?php var_dump($dish) ?>
<?php endforeach ?>

Resources