Cakephp join table in my View - cakephp

I need you help please!
My view didn't work with my INNER JOIN
public function view($bk_id = null)
{
$data = $this->Book->findBybk_id($bk_id, array(
'recursive' => -1,
'fields' => array('Book.*', 'Article.*'),
'joins' => array(
array(
'table' => 'Articles',
'alias' => 'Article',
'type' => 'INNER',
'conditions' => array('Article.bk_id = Book.bk_id')
)
)
));
$this->set('Book', $data);
}
Can anyone help me?

You appear to be using Cake's findBy magic functions which don't take a second parameter. Try using find('first') with the relevant conditions instead:-
$data = $this->Book->find('first', array(
'recursive' => -1,
'fields' => array('Book.*', 'Article.*'),
'joins' => array(
array(
'table' => 'Articles',
'alias' => 'Article',
'type' => 'INNER',
'conditions' => array('Article.bk_id = Book.bk_id')
)
),
'conditions' => array(
'Book.bk_id' => $bk_id
)
));

Related

cakePHP Paginate and sort by DESC

At the moment i query my db with this query below and it works great , it joins with another table.
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'limit' => 15
)
);
I have setup a Pagenator :
public $paginate = array (
'order' => array('Tapplicant.AppID' => 'desc'),
'limit' => 15,
);
What i need to know is how do i :
Add Paginator
Using on the current date CURDATE()
I jsut dont know where to add it in.
In your controller:-
$this->paginate = array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'conditions' => array(
'Tapplicant.AppDate' => date('Y-m-d')
),
'order' => array('Tapplicant.AppDate' => 'DESC'),
'limit' => 15
);
$this->set('tapplicant', $this->paginate());
You can do this right in the find:
$tapplicant = $this->Tapplicant->find(
'all',
array(
'fields' => array(
'Tapplicant.*',
'Toutcome.*'
),
'joins' => array(
array(
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'INNER',
'conditions' => array('Tapplicant.AppID = Toutcome.AppID' )
)
),
'limit' => 15,
'order' => array('Tapplicant.AppID' => 'desc'),
)
);
And leave paginate like this:
public $paginate = array('limit' => 15);
If you only want the posts from the current day, just add this to the find:
'conditions' => array('Tapplicant.created' => date('Y-m-d')),

Join and Count Tables in cakePHP

thanks to the help i have gotten on Stackoverflow while learning php i am able to Join tables and use Count.
I am unable to do both in one query.
I am wanting to count records in a joined table.
This is what i have tryed and i seem to get errors:
$options = array(
'fields' => array(
'toutcome.AffCommission',
),
'joins' => array(
array(
'conditions' => array(
'tapplicant.AppID = toutcome.AppID',
),
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'join',
),
),
'limit' => 'Toutcome',
'offset' => 'Toutcome',
'contain' => array(
'Toutcome',
),
);
$data = $this->Tapplicant->find('count', $options);
$this->set('count', $data );
Try this
$options = array(
'fields' => array(
'toutcome.AffCommission',
),
'joins' => array(
array(
'conditions' => array(
'tapplicant.AppID = toutcome.AppID',
),
'table' => 'toutcome',
'alias' => 'Toutcome',
'type' => 'join',
),
),
'limit' => n, // its should be integer
'offset' => n, // its should be integer
'contain' => array(
'Toutcome',
),
);

How to assign mysql functions on join fields in Cakephp

I want to get last_deadline and the count of instalments of all instalments but obviously this query will show me 1 order and the last_deadline.
$orders = $this->find('all', array(
'fields' => array(
'Order.order_id', 'Order.summary', 'Order.fee',
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline'
),
'conditions' => $conditions,
'joins' => array(
array(
'table' => $this->getTableName('default', 'brief_instalments'),
'alias' => 'BriefInstalment',
'type' => 'RIGHT',
'conditions' => array(
'Order.order_id = BriefInstalment.order_id',
'BriefInstalment.deleted' => 0,
),
'order' => 'BriefInstalment.deadline ASC',
)
),
'order' => 'BriefInstalment.deadline ASC'
));
I have tried 'contain' and doesn't work.
'contain' => array(
'BriefInstalment' => array(
'fields' => 'BriefInstalment.id',
'fields' => array(
'BriefInstalment.id',
'MAX(`BriefInstalment`.`deadline`) AS last_deadline', 'COUNT(`BriefInstalment`.`id`) AS total_instalments'
),
'conditions' => array(
'BriefInstalment.deleted' => 0
)
)
),
By the way I don't want to use loop to get last_intalments and cout brief_instalments. e.g.
// Determine deadlines
foreach ($orders as $i => $order) {
$deadline = $this->BriefInstalment->getLastDeadline($order['Order']['order_id']);
$orders[$i] += array(
...
'last-deadline' => $deadline,
'total-instalments' => count($order['BriefInstalment'])
);
}
The reason is it decrease the speed of loading.
Any help plz
Here is how to create custom query
$conditionsSubQuery['"User2"."status"'] = 'B';
$db = $this->User->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"User2"."id"'),
'table' => $db->fullTableName($this->User),
'alias' => 'User2',
'limit' => null,
'offset' => null,
'joins' => array(),
'conditions' => $conditionsSubQuery,
'order' => null,
'group' => null
),
$this->User
);
$subQuery = ' "User"."id" NOT IN (' . $subQuery . ') ';
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$this->User->find('all', compact('conditions'));
Reference:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#sub-queries

Cakephp Find conditions on related table with hasAndBelongsToMany

I have the following code that gives an error from the moment I do a find condition on the associated Model (HABTM):
class Users extends Model{
public $useTable = 'users';
public $hasAndBelongsToMany = array(
'UserCategories' => array(
'className' => 'UserCategories',
'joinTable' => 'user_categories_link',
'foreignKey' => 'user_id',
'associationForeignKey' => 'category_id',
'unique' => false,
)
);
public function getData($page = 0, $category = '', $subcategory = ''){
return $this->find('all', array(
'limit' => 6,
'page' => $page,
'conditions'=> array(
'active'=> 1,
'UserCategories.category_name' => $category, // THIS GIVES ERROR
'UserCategories.category_subcategory' => $subcategory, // THIS GIVES ERROR
)
));
}
In my Controller:
$this->Users->getData(0, 'somemaincategory', 'somesubcategory');
I can't seem to do conditions on the related HABTM-Model (UserCategories in this case). I also tried to use 'contain' (with $actsAs), but then he stills gives me all the User data even if there is no Category linked with it. The Category array is in that case just blank.
Hope someone can help me.
Thanks in advance,
AƤron
Do a manual join. You can use this to do an actual inner join (contain will act as a left join). http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#joining-tables
$this->find('all',
array(
'conditions' => array(
'active' => 1,
),
'joins' => array(
array(
'table' => 'user_categories_link',
'alias' => 'UserCategoriesLink',
'type' => 'inner',
'conditions' => array(
'UserCategoriesLink.user_id = User.id'
),
),
array(
'table' => 'user_categories',
'alias' => 'UserCategories',
'type' => 'inner',
'conditions' => array(
'UserCategories.id = UserCategoriesLink.category_id',
'UserCategories.category_name' => $category,
'UserCategories.category_subcategory' => $subcategory,
),
)
),
)
);

CakePHP join issue

This is driving me crazy. This is not throwing any errors but it is also not performing the joins. I'm hoping that this is one where I've spent too long looking at it and the answer is obvious to someone else...
$lines = $this->RevenueLine->find('all', array(
'conditions' => array(
'RevenueLine.is_triggered' => 1,
'RevenueLine.date_triggered >=' => $sqldate1,
'RevenueLine.date_triggered <=' => $sqldate2,
),
'joins' => array(
array(
'table' => 'projects',
'alias' => 'Project',
'type' => 'INNER',
'conditions' => array(
'RevenueLine.project_id = Project.id'
)
),
array(
'table' => 'clients',
'alias' => 'Client',
'type' => 'INNER',
'conditions' => array(
'Project.client_id = Client.id'
)
),
array(
'table' => 'classifications',
'alias' => 'Classification',
'type' => 'INNER',
'conditions' => array(
'Project.classification_id = Classification.id'
)
)
),
'order' => array(
'Client.client_number ASC',
'Project.pn_counter ASC'
)
)
);
You need to select the fields from the joined tables:
'fields' => array(
'JoinTable1.*',
'JoinTable2.*',
'JoinTable3.*',
'JoinTable4.*'
)
as a parameter of your find.

Resources