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.
Related
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
)
));
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')),
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',
),
);
I am trying to get the employees and their posting details with largest posting_from date.
My query is:
$emp = $this->EmployeePersonal ->find(
'all',
array(
'fields' => array('EmployeePersonal.*', 'PermanentDist.name','PresentDist.name','EmployeePosting.*','Designation.name','Department.name','Office.name' ),
'conditions' => $condition,
//'order' => array('Designation.id'),
'group' => 'EmployeePersonal.id',
'order' => 'EmployeePosting.posting_from DESC',
'recursive' => -1,
'joins' => array(
array(
'table' => 'employee_postings',
'alias' => 'EmployeePosting',
'type' => 'LEFT',
'conditions' => array(
'EmployeePosting.employee_personal_id = EmployeePersonal.id',
)
),
)
)
);
But the above query shows the lowest posting_from value. Why isn't the order working in my case?
some commas and will be ready:
$emp = $this->EmployeePersonal ->find(
'all',
array(
'joins' => array(
array(
'table' => 'employee_postings',
'alias' => 'EmployeePosting',
'type' => 'LEFT',
'conditions' => array(
'EmployeePosting.employee_personal_id = EmployeePersonal.id'
)
)
),
'fields' => array('EmployeePersonal.*', 'PermanentDist.name','PresentDist.name','EmployeePosting.*','Designation.name','Department.name','Office.name'),
'conditions' => $condition,
//'order' => array('Designation.id'),
'group' => 'EmployeePersonal.id',
'order' => array(
'EmployeePosting.posting_from' => 'DESC'
),
'recursive' => -1
)
);
I have these tables in my database: 'products' belongs to a 'company', and 'company' has many 'company_addresses'. And I am building a search action in my controller which will return the products based on user's inputted company address. How can I get something like the followin?
SELECT *
FROM products
LEFT JOIN companies ON products.company_id = companies.id
LEFT JOIN company_addresses ON companies.id = company_addresses.company_id
WHERE company_addresses.address1 LIKE '%'.$this->data['Product']['keyword'].'%'
OR company_addresses.address2 LIKE '%'.$this->data['Product']['keyword'].'%'
This doesn't seem to work:
$results = $this->Product->find('all', array(
'conditions' => array(
'OR' => array(
array('CompanyAddress.address1 LIKE' => '%'.$this->data['Product']['keyword'].'%'),
array('CompanyAddress.address2 LIKE' => '%'.$this->data['Product']['keyword'].'%')
)
),
'order' => array(
'Product.id' => 'ASC'
),
'contain' => array(
'Company' => array(
'CompanyAddress' => array(
'State',
'City'
)
)
)
));
Nor this:
$results = $this->Product->find('all', array(
'order' => array(
'Product.id' => 'ASC'
),
'contain' => array(
'Company' => array(
'CompanyAddress' => array(
'conditions' => array(
'OR' => array(
array('CompanyAddress.address1 LIKE' => '%'.$this->data['Product']['keyword'].'%'),
array('CompanyAddress.address2 LIKE' => '%'.$this->data['Product']['keyword'].'%')
)
),
'State',
'City'
)
)
)
));
I think the best for you is to use the join of CakePHP.
Something like this:
$this->Product->find('all', array(
'joins' => array(
array(
'table' => 'companies',
'alias' => 'Company',
'type' => 'left',
'conditions' => array(
'Company.id = Product.company_id'
),
),
array(
'table' => 'companies_addresses',
'alias' => 'CompaniesAddress',
'type' => 'left',
'conditions' => array(
'CompaniesAdress.company_id = Company.id',
'OR' => array(
'CompanyAdress.address1 LIKE' => '%' . $this->data['Product']['keyword'] . '%',
'CompanyAdress.address2 LIKE' => '%' . $this->data['Product']['keyword'] . '%',
),
),
),
),
));