How to make join with find? - cakephp-2.0

I have a table that contains some foreign keys. The table is: matriculas. In this table I have a foreign key to table pessoas and in this table pessoas has a foreign key to table tipopessoas.
I'm trying create a JOIN to return informations of tipopessoas, but I can't do this.
How could I do this ?
I need this
SELECT * FROM pessoas t1
INNER JOIN tipopessoas t2 ON (t1.tipopessoas_id = t2.id)
WHERE t2.descrica = "ALUNO";
I'm trying this.
public function add() {
if ($this->request->is('post')) {
$this->Matricula->create();
if ($this->Matricula->save($this->request->data)) {
$this->Session->setFlash(__('The disciplina has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('The disciplina could not be saved. Please, try again.'));
}
$this->set("pessoas", $this->Matricula->Pessoa->find("list",array(
"fields"=>array("nome"),
"join"=>array("table"=>"tipopessoas",
"alias"=>"tipo",
"type"=>"left",
"condition"=>array(
"Pessoa.tipopessoas_id = " => "tipo.id",
"tipo.descricao = " => "ALUNO"
))
)
));
}
here workbench project

Hello #FernandoPaiva can you please update you find query with this:
$this->Pessoa->find('all', array("fields" => "Pessoa.*, Tipopessoa.*","conditions" => array("Tipopessoa.descrica" => "ALUNO"),
"joins" => array(array(
"table" => "tipopessoas",
"alias" => "Tipopessoa",
"type" => "inner",
"condition" => array("Tipopessoa.id" => "Pessoa.tipopessoas_id"),
))));
Hope this may worked for you...

I managed to solve it !
I did
Controller
public function add() {
if ($this->request->is('post')) {
$this->Matricula->create();
if ($this->Matricula->save($this->request->data)) {
$this->Session->setFlash(__('The disciplina has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('The disciplina could not be saved. Please, try again.'));
}
$this->set("pessoas", $this->Matricula->Pessoa->find('list', array(
'fields' => array("Pessoa.id", "Pessoa.nome", "Pessoa.tipopessoas_id",'Tipopessoa.id','Tipopessoa.descricao'),
'conditions' => array('Tipopessoa.descricao'=>'ALUNO'),
'recursive' => 0)));
}
View
<div class="form-group">
<?php echo $this->Form->input("pessoas_id", array("empty"=>"Escolha uma opção",
"style"=>"width:200px",
"class"=>"form-control",
));?>
</div>

Related

Create new hasMany children from parent controller

I have two controllers "Events" and "Activities" and both have many "Attendees".
$this->hasMany('Attendees')
->setClassName('Attendees')
->setForeignKey('foreign_id')
->setConditions(array('Attendees.class' => 'Activity'))
->setDependent(true);
I am using a class and a foreign_id in my Attendees table to link them. I would like to create addAttendee() function in my ActivitiesController for example to add a new attendee, but I am not sure how to proceed.
public function addAttendee($id = null)
{
$activity = $this->Activities->get($id, ['contain' => ['Venues', 'Contacts']]);
if ($this->request->is('post'))
{
??
}
$this->set(compact('activity'));
}
I found some documentation on saving with association but not on creating new association.
You can create a new Attendee entity then link it to your $activity.
$data = ['first_name' => 'blah', 'last_name' => ... fields]);
$attendee = $this->Attendee->newEntity($data);
$this->Attendee->link($activity, [$attendee]);
source: https://api.cakephp.org/4.2/class-Cake.ORM.Association.HasMany.html#link()
I proceed this way. It's working but not sure if it's the right way to proceed ??
public function addAttendee($id)
{
$activity = $this->Activities->get($id, ['contain' => ['Venues', 'Contacts']]);
$attendee = $this->Activities->Attendees->newEmptyEntity();
if ($this->request->is('post'))
{
$attendee = $this->Activities->Attendees->patchEntity($attendee, $this->request->getData() + ['class' => 'retreat', 'foreign_id' => $id]);
if ($this->Activities->Attendees->save($attendee))
{
$this->Flash->success(__('The attendee has been saved.'));
return $this->redirect(['action' => 'view', $id]);
}
$this->Flash->error(__('The attendee could not be saved. Please, try again.'));
}
$this->set(compact('activity', 'attendee'));
}

Validation not working with saveMany

Validations are not working with saveMany for all values.its only working for first value. I am using translate behavior.
My code:
public function admin_add() {
if ($this->request->is('post')) {
$this->FaqCategory->create();
$this->request->data['FaqCategory']['name'] = Inflector::slug($this->request->data['FaqCategory']['name'], $replacement = '-');
if ($this->FaqCategory->saveMany($this->request->data)) {
$this->Session->setFlash('The faq category has been saved', 'default', array('class' => 'success'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The faq category could not be saved. Please, try again.'));
}
}
$languages = $this->Language->getlangs();
$this->set('langs', $languages);
}
Try this: $this->FaqCategory->saveMany($this->request->data, array('validate' => true));

Cakephp display second model association name instead of ID

I have the next tables in the database:
Lift
Vehicle
Manufacturer
Lift belongsTo Vehicle
Vehicle belongsTo Manufacturer
Manufacturer hasMany Vehicles
LiftsController.php add method:
public function add() {
if ($this->request->is('post')) {
$this->Lift->create();
$this->request->data['Lift']['user_id'] = $this->Auth->user('id');
if ($this->Lift->save($this->request->data)) {
$this->Session->setFlash(__('The lift has been saved'));
$this->redirect(array('action' => 'my_lifts'));
} else {
$this->Session->setFlash(__('The lift could not be saved. Please, try again.'));
}
}
$vehicles = $this->Lift->Vehicle->find('all', array('fields' => array('id', 'model', 'manufacturer_id')));
$towns = $this->Lift->TownFrom->find('list', array('order' => 'county ASC, name ASC', 'fields' => array('id', 'name', 'county')));
$this->set(compact('users', 'vehicles', 'towns'));
}
LiftsController.php add view:
echo $this->Form->input('vehicle_id');
A printscreen:
So what I need is to display instead of Manufacturer ID's the manufacturer name.
In every model the $displayField is set to the correct value
Try changing:
echo $this->Form->input('vehicle_id');
To
echo $this->Form->input('Vehicle');
Change
$vehicles = $this->Lift->Vehicle->find('all', array('fields' => array('id', 'model', 'manufacturer_id')));
To
$vehicles = $this->Lift->Vehicle->find('list', array('conditions' => array('Vehicle.user_id' => $this->Auth->user('id')), 'recursive' => 0, 'fields' => array('Vehicle.id', 'Vehicle.model', 'Manufacturer.title')));

Cakephp problem with Save belongsTo

I got problem when save belongto
class Script extends AppModel {
var $name = 'Script';
var $belongsTo = array(
'ScriptCatagories' => array(
'className' => 'ScriptCatagories',
'foreignKey' => 'script_catagories_id',
function add_script() {
pr($this->data);
if (!empty($this->data)) {
$this->Script->create();
$this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;
$this->data['Script']['user_id'] = $this->Auth->user('id');
if ($this->Script->save($this->data)) {
$this->Session->setFlash(__('The script has been saved', true));
$this->redirect(array('action' => 'script_index'));
} else {
$this->Session->setFlash(__('The script could not be saved. Please, try again.', true));
}
}
pr($scriptCatagories = $this->Script->ScriptCatagories->find('list'));
$this->set(compact('scriptCatagories'));
}
when click save
Array
(
[Script] => Array
(
[name] => fdfvvds
[description] => dfvdfvdfvdf
[tags] =>
)
)
Array
(
[0] => performance
[1] => cleanup
)
question in
$this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;
I need to save categoryid to script table but I don't know to get it
thank you
var $belongsTo = array(
'ScriptCatagories' => array(
'className' => 'ScriptCatagories',
'foreignKey' => 'script_catagories_id',
first, it doesn't have close parentheses. And it should be singular ScriptCategory, also the foreign key script_category_id (just to follow the convention). And you misspelled category (you have $this->Script->scriptCategory->id; later in the code)

CakePHP - Multiple row edits

How can I do edits to a table on multiple rows?
I followed the tutorial here http://matsimitsu.com/blog/2008/01/06/saveall-with-cakephp.html but it doesn't seem to work.
Here is what I'm doing, but it's not working.
Thanks,
Tee
function editAll() {
$this->data = $this->Settings->find('all', array('conditions' => $conditions));
}
Then in the view, this is what I have
foreach ($this->data as $setting):
echo $form->input('Setting.' . $setting['Setting']["id"] . '.value', array('value' => $setting['Setting']["value"]));
endforeach;
Then in the add function I have
function add() {
if (!empty($this->data)) {
$this->Setting->create();
if ($this->Setting->saveAll($this->data)) {
$this->Session->setFlash(__('The Setting has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Setting could not be saved. Please, try again.', true));
}
}
}
You need to include the id field, so the data in your controller will look like this:
'Setting' => array(
0 => array(
'id' => 42,
'value' => 'foo'
),
1 => array(…)
)
So in the view, do this:
foreach ($this->data as $i => $setting) {
echo $this->Form->hidden("Setting.$i.id", array('value' => $setting['Setting']['id']));
echo $this->Form->input("Setting.$i.value", array('value' => $setting['Setting']['value']));
}

Resources