cake php views from belongsTo association - cakephp

i have two tables with the following columns, guardians(id,student_no) and student(id,admission_no). student_no is foreign to admision_no. students has a hasMany association with guardians, guardians has a belongsTo association with students.
here are my models
STUDENT
public $hasMany = array(
'Guardian' => array(
'className' => 'Guardian',
'foreignKey' => 'student_no',
'dependent' => true,
)
)
GUARDIAN
public $belongsTo = array(
'Student' => array(
'className' => 'Student',
'foreignKey' => 'student_no',
)
)
Guardian controller
public function view($id = null) {
if (!$this->Guardian->exists($id)) {
throw new NotFoundException(__('Invalid guardian'));
}
$options = array('conditions' => array('Guardian.' . $this->Guardian->primaryKey => $id));
$this->set('guardian', $this->Guardian->find('first', $options));
}
Guardian view.ctp
(truncated to view only the associated student from within the Guardian model)
<h3><?php echo __('Associated Students'); ?></h3>
<?php if (!empty($guardian['Student'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Admission No'); ?></th>
<th><?php echo __('First Name'); ?></th>
<th><?php echo __('Last Name'); ?></th>
<th><?php echo __('Gender'); ?></th>
<th><?php echo __('Date Of Birth'); ?></th>
<th><?php echo __('Join Date'); ?></th>
<th><?php echo __('Form'); ?></th>
<th><?php echo __('Student Class'); ?></th>
<th><?php echo __('Middle Name'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($guardian['Student'] as $student): ?>
<tr>`
<td><?php echo $student['admission_no']; ?></td>
line(115) <td><?php echo $student['first_name']; ?></td>
<td><?php echo $student['last_name']; ?></td>
<td><?php echo $student['gender']; ?></td>
<td><?php echo $student['date_of_birth']; ?></td>
<td><?php echo $student['join_date']; ?></td>
<td><?php echo $student['form']; ?></td>
<td><?php echo $student['student_class']; ?></td>
<td><?php echo $student['middle_name']; ?></td>
</tr>
<?php endforeach; ?>
</table>
i can view associated parent details from students view with above similar code,
however in guardians view i get error for the associated guardian
ERROR:Warning (2): Illegal string offset 'first_name' [APP/View/Guardians/view.ctp, line 115]
and for three lines below it.what exactly is going wrong

Do some debugging: debug($guardian)
In your belongsTo assocaition, a guardian can only have a single student, so you are iterating over the columns of that one single student, ie consequently the $student variable will be a string, hence the error.
See also Cookbook > Models > Associations > belongsTo

Related

How to insert multiple table for one form add in cakephp

so i want to create one form add for 3 tables, but i dont understand the scipt, i am googling but, i dont know, can anyone help me?
Table pln_nontaglish_suspect
| id | trx_id | no_regristrasi | tgl_regristrasi | nama | id_pel | rptag | switching_ref | admin_charge | iso | xml | inbox_id | telepon | info | jenis_transaksi | tagihan | partnet_cid | merchant | pln_ref | bank_code |dt_trx | reprint | create_date|
Table Mutations
| id | amount | note | jenis | inbox_id | balance | create_date | create_by | update_date | update_by | user_id | version |
Table Transactions
| id | user_id | product_id | destination | sender | sender_type | create_date | price_sell | price_buy | inbox_id | status | remark | saldo_awal | sn | receiver | resend
in the form add.ctp
<div id="page-container" class="row">
<div id="page-content" class="col-sm-9">
<!--<h2><?php echo __('Add Pln Nontaglist Suspect'); ?></h2>-->
<div class="plnNontaglistSuspects form">
<?php echo $this->Form->create('PlnNontaglistSuspect', array('role' => 'form')); ?>
<table>
<div style="display:none">
<?php
$now = new DateTime();
$today = $now->format("Y-m-d H:i:s");
$user = $this->Session->read('Auth.User.username');
$id = 'user_id';
$user_id = $_POST['Transaction.user_id'];
$inbox_id = $_POST['PlnNontaglistSuspect.inbox_id'];
echo $this->Form->input('PlnNontaglistSuspect.trx_no');
echo $this->Form->input('PlnNontaglistSuspect.idpel');
echo $this->Form->input('PlnNontaglistSuspect.rptag');
echo $this->Form->input('PlnNontaglistSuspect.admin_charge');
echo $this->Form->input('PlnNontaglistSuspect.xml');
echo $this->Form->input('PlnNontaglistSuspect.telpon');
echo $this->Form->input('PlnNontaglistSuspect.info');
echo $this->Form->input('PlnNontaglistSuspect.merchant');
echo $this->Form->input('PlnNontaglistSuspect.pln_ref');
echo $this->Form->input('PlnNontaglistSuspect.bank_code');
echo $this->Form->input('PlnNontaglistSuspect.dt_trx');
echo $this->Form->input('PlnNontaglistSuspect.reprint');
echo $this->Form->input('PlnNontaglistSuspect.create_date', array('value' => $today));
//input for table mutations
echo $this->Form->input('Mutation.note');
echo $this->Form->input('Mutation.jenis');
echo $this->Form->input('Mutation.inbox_id', array('value' => $inbox_id));
echo $this->Form->input('Mutation.balance');
echo $this->Form->input('Mutation.create_date', array('value'=>$today));
echo $this->Form->input('Mutation.create_by', array('value' => $user));
echo $this->Form->input('Mutation.update_date');
echo $this->Form->input('Mutation.update_by');
echo $this->Form->input('Mutation.user_id', array('value' => $user_id));
echo $this->Form->input('Mutation.version');
//input for table transactions
//echo $this->Form->input('Transaction.user_id', array('value' => $id));
echo $this->Form->input('Transaction.destination');
echo $this->Form->input('Transaction.sender');
echo $this->Form->input('Transaction.sender_type');
echo $this->Form->input('Transaction.create_date', array('value' => $today ));
echo $this->Form->input('Transaction.price_buy');
echo $this->Form->input('Transaction.status');
echo $this->Form->input('Transaction.saldo_awal');
echo $this->Form->input('Transaction.sn');
echo $this->Form->input('Transaction.receiver');
echo $this->Form->input('Transaction.resend');
?>
</div>
<legend>Tambah Nontaglish</legend>
<tr>
<td>No Regristrasi</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.no_registrasi', array('label' => false,'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Tgl Regristrasi</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.tgl_registrasi', array('label' => false, 'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Inbox Id</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.inbox_id', array('label' => false, 'type' => 'text', 'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Produk Id</td>
<td> : </td>
<td><?php echo $this->Form->input('Transaction.product_id', array('label' => false, 'empty' => '----Select Produk Id----', 'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>User Id</td>
<td> : </td>
<td><?php echo $this->Form->input('Transaction.user_id', array('label' => false, 'empty' => '----Select User Id----','class' => 'form-control'));?></td>
</tr>
<tr>
<td>Nama Pelanggan</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.nama', array('label' => false, 'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Price Sell</td>
<td> : </td>
<td><?php echo $this->Form->Input('Transaction.price_sell', array('label' => false, 'class' => 'form-control'));?></td>
</tr>
<tr>
<td>Iso</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.iso', array('label' => false,'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Switching Ref</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.switching_ref', array('label' => false, 'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Tagihan</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.tagihan', array('label' => false,'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Partner Cid</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.partner_cid', array('label' => false, 'class' => 'form-control')); ?></td>
</tr>
<tr>
<td>Amount</td>
<td> : </td>
<td><?php echo $this->Form->input('Mutation.amount', array('label' => false, 'class' => 'form-control'));?></td>
</tr>
<tr>
<td>Remark</td>
<td> : </td>
<td><?php echo $this->Form->input('Transaction.remark', array('label' => false, 'class' => 'form-control'));?></td>
</tr>
<tr>
<td>Jenis Transaksi</td>
<td> : </td>
<td><?php echo $this->Form->input('PlnNontaglistSuspect.jenis_transaksi', array('label' => false,'class' => 'form-control')); ?></td>
</tr>
</table>
<?php echo $this->Form->submit('Submit', array('class' => 'btn btn-large btn-primary')); ?>
<?php echo $this->Form->end(); ?>
</div><!-- /.form -->
</div><!-- /#page-content .col-sm-9 -->
And In the Controller
public function add() {
$this->loadModel('Product');
$this->loadModel('User');
if ($this->request->is('post')) {
$this->PlnNontaglistSuspect->create();
if ($this->PlnNontaglistSuspect->save($this->request->data)) {
$this->Session->setFlash(__('The pln nontaglist suspect has been saved'), 'flash/success');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The pln nontaglist suspect could not be saved. Please, try again.'), 'flash/error');
}
}
//$inboxes = $this->PlnNontaglistSuspect->Inbox->find('list');
$products = $this->Product->find('list');
$users = $this->User->find('list',array(
'fields'=>array('User.id','User.username'),
'order'=>array('User.username')
)
);
$this->set(compact('products','users'));
}
Having had a closer look your Models do not appear to be related, so saveAll or saveAssociated will not work in this case.
If you are in the PlnNontaglistSuspect Model you will need to load up your Mutation and Transaction Models and save them separately.
Example code:
public function add() {
//only try and save when the user has posted something
if ($this->request->is('post')) {
//in this example we are in the Mutation model
// so load up Suspect and Transaction
$this->loadModel('Suspect');
$this->loadModel('Transaction');
//create and save our Mutation record
$this->Mutation->create();
$this->Mutation->save($this->request->data);
//create and save our Suspect record
$this->Suspect->create();
$this->Suspect->save($this->request->data);
//create and save our Transaction record
$this->Transaction->create();
$this->Transaction->save($this->request->data);
//set a Flash message and redirect the user
$this->Session->setFlash(__('The mutation has been saved.'));
return $this->redirect(array('action' => 'index'));
}//end of if is post conditional
}//end of add function
(Please note this is not production ready - you need to add error handling in case any one of the three Models doesn't save. As you are saving three separate Models in one operation you will need to give some thought to how to approach this).
If the Models were related you could use a single saveAssociated or saveAll method as described here: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
One final point - you will make your life a lot easier if you stick to Cake conventions. For example one of your tables is pln_nontaglish_suspect, consider renaming this to pln_nontaglish_suspects as Cake expects plural table names by convention. Also, you have a create_date column in your tables, if you change this to created (datetime) Cake will automagically populate this with the time at save, likewise you can add a modified (datetime) column and Cake will record the time of any updates in this column.
More here:
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html
And:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#using-created-and-modified
Cake becomes far more powerful when you use the conventions and helpers built into it.

Cake PHP display field

I have 4 tables named customers, calls, employees and companies. Customers belong to a company and an employee. Calls belong to a customer and an employee. At the moment in my call index file it shows the relevant customer name and employee name. I want to display, for a call the relevant customer name, the customers company name they belong to and the employee name. I don't know how to display the relevant company name based on the customer. Can someone help? This is my code:
call/ index.ctp
<?php
$usertype=$this->SESSION->read('User.usertype');
?>
<div class="calls index">
<h2><?php echo __('Call Details'); ?> </h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Call Time'); ?></th>
<th><?php echo $this->Paginator->sort('Comments'); ?></th>
<th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
<th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php foreach ($calls as $call): ?>
<tr>
<td><?php echo h($call['Call']['id']); ?> </td>
<td><?php echo h($call['Call']['call_date']); ?> </td>
<td><?php echo h($call['Call']['call_time']); ?> </td>
<td><?php echo h($call['Call']['comments']); ?> </td>
<td><?php echo h($call['Call']['next_call_date']); ?> </td>
<td>
<?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
</td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
</td>
</tr>
callsController:
class CallsController extends AppController {
/**
* Components
*
* #var array
*/
public $components = array('Paginator');
/**
* index method
*
* #return void
*/
public function index() {
$this->Call->recursive = 0;
$this->set('calls', $this->Paginator->paginate());
}
/**
* view method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function view($id = null) {
if (!$this->Call->exists($id)) {
throw new NotFoundException(__('Invalid call'));
}
$options = array('conditions' => array('Call.' . $this->Call->primaryKey => $id));
$this->set('call', $this->Call->find('first', $options));
}
/**
* add method
*
* #return void
*/
public function add() {
if ($this->request->is('post')) {
$this->Call->create();
if ($this->Call->save($this->request->data)) {
$this->Session->setFlash(__('The call has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The call could not be saved. Please, try again.'));
}
}
$customers= $this->Call->Customers->find('list',array('order'=>'customer_name ASC','fields'=>array('id','customer_name')));
$employees= $this->Call->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));
$this->set(compact('customers', 'employees'));
}
/**
* edit method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function edit($id = null) {
if (!$this->Call->exists($id)) {
throw new NotFoundException(__('Invalid call'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->Call->save($this->request->data)) {
$this->Session->setFlash(__('The call has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The call could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('Call.' . $this->Call->primaryKey => $id));
$this->request->data = $this->Call->find('first', $options);
}
$customers= $this->Call->Customers->find('list',array('order'=>'customer_name ASC','fields'=>array('id','customer_name')));
$employees= $this->Call->Employees->find('list',array('order'=>'employee_name ASC','fields'=>array('id','employee_name')));
$this->set(compact('customers', 'employees'));
}
/**
* delete method
*
* #throws NotFoundException
* #param string $id
* #return void
*/
public function delete($id = null) {
$this->Call->id = $id;
if (!$this->Call->exists()) {
throw new NotFoundException(__('Invalid call'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Call->delete()) {
$this->Session->setFlash(__('The call has been deleted.'));
} else {
$this->Session->setFlash(__('The call could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}}
the array looks like this:
Array
(
[Call] => Array
(
[id] => 7
[call_date] => 2013-04-27
[call_time] => 08:31:00
[comments] => Require installation
[next_call_date] => 2014-04-27
[customers_id] => 2
[employees_id] => 3
)
[Customers] => Array
(
[id] => 2
[customer_name] => Snith Jams
[customer_address] => 192 Waverley Road
[customer_suburb] => Caulfield East
[customer_state] => VIC
[customer_postcode] => 3145
[customer_dob] => 2014-04-09
[customer_anniversary] => 2014-04-10
[customer_phone1] => 0492832921
[customer_phone2] => 0392817894
[customer_phone3] => 0482938281
[customer_fax] =>
[customer_email] => tsmith#hotmail.com
[customer_gender] => M
[customer_type] => Silver
[customer_PW] => a1c680c2bfcca40816dd81eff3980cc9828c9088
[customer_username] => samman
[companies_id] => 3
[employees_id] => 11
)
[Employees] => Array
(
[id] => 3
[employee_name] => Jones
[date_hired] => 2013-02-04
[employee_phone_number] => 0449997582
[employee_email] => indianajones#gmail.com
[employee_address] => 22 Queens street Melbourne CBD
[employee_dob] => 1966-01-31
[access_level] => staff
[employee_username] => jones12
[employee_pw] => 603fce7dcbec3c9cba24e87d058a3341e37779b8
)
)
callsController:
public $components = array('Paginator');
public function index() {
$this->Call->recursive = 2;
$this->set('calls', $this->Paginator->paginate());
}
calls/index.ctp:
<div class="calls index">
<h2><?php echo __('Call Details'); ?> </h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Call Time'); ?></th>
<th><?php echo $this->Paginator->sort('Comments'); ?></th>
<th><?php echo $this->Paginator->sort('Next Call Date'); ?></th>
<th><?php echo $this->Paginator->sort('Customer Name'); ?></th>
<th><?php echo $this->Paginator->sort('Company Name'); ?></th>
<th><?php echo $this->Paginator->sort('Employee Name'); ?></th>
<th class="actions"><?php echo __(''); ?></th>
</tr>
<?php foreach ($calls as $call): ?>
<tr>
<td><?php echo h($call['Call']['id']); ?> </td>
<td><?php echo h($call['Call']['call_date']); ?> </td>
<td><?php echo h($call['Call']['call_time']); ?> </td>
<td><?php echo h($call['Call']['comments']); ?> </td>
<td><?php echo h($call['Call']['next_call_date']); ?> </td>
<td>
<?php echo $this->Html->link($call['Customers']['customer_name'], array('controller' => 'customers', 'action' => 'view', $call['Customers']['id'])); ?>
</td>
<td>
<?php echo $this->Html->link($call['Customers']['Companies']['company_name']); ?>
</td>
<td>
<?php echo $this->Html->link($call['Employees']['employee_name'], array('controller' => 'employees', 'action' => 'view', $call['Employees']['id'])); ?>
</td>
<?php echo pr($call); ?>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $call['Call']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
just set recursive to 2
$this->Call->recursive = 2;
(or $this->Calls->recursive = 2 it depends if your model is Call or Calls)
and you can access to Company information like this
$call['Customer']['Company']['name']
Because it seems you are not following cake naming conventions (you are using plural for Models' names) maybe you'll need to use
$call['Customers']['Companies']['name']
I suggest you to follow the conventions, anyway if you print your array doing pr($call); you'll see how the array is structured and how to access your data

Cakephp Association Property belongs to Image but also has many images

I am currently trying to solve a problem which I just noticed since the property didn't actually have a main_image allocated to it.
But as soon as I allocated a main image to it. The Related images caused problems.
Property table has a belongsTo relation
public $belongsTo = array( 'Image' => array(
'className' => 'Image',
'foreignKey' => 'main_image_id'
));
and also a hasMany
public $hasMany = array(
'Image' => array(
'className' => 'Image',
'foreignKey' => 'property_id'
)
);
the problem is when I am populating the related images I am receiving multiple errors similar to the below
Illegal string offset 'id'
my data is like so
property(array)
Image(array)
id1
descriptionPainted Brick Design
imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
property_id3
0(array)
id1
descriptionPainted Brick Design
imageuploads/properties/3/Amazing-Painted-Brick-Houses-Design.jpeg
property_id3
and the view was generated like so
<div class="related">
<h3><?php echo __('Related Images'); ?></h3>
<?php if (!empty($property['Image'])): ?>
<table cellpadding = "0" cellspacing = "0">
<tr>
<th><?php echo __('Id'); ?></th>
<th><?php echo __('Description'); ?></th>
<th><?php echo __('Image'); ?></th>
<th><?php echo __('Property Id'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php foreach ($property['Image'] as $image): ?>
<tr>
<td><?php echo $image['id']; ?></td>
<td><?php echo $image['description']; ?></td>
<td><?php echo $image['image']; ?></td>
<td><?php echo $image['property_id']; ?></td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('controller' => 'images', 'action' => 'view', $image['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('controller' => 'images', 'action' => 'edit', $image['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('controller' => 'images', 'action' => 'delete', $image['id']), null, __('Are you sure you want to delete # %s?', $image['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<div class="actions">
<ul>
<li><?php echo $this->Html->link(__('New Image'), array('controller' => 'images', 'action' => 'add')); ?> </li>
</ul>
</div>
</div>
I am not sure if maybe the relations are setup incorrectly or that maybe I would have to include an if to see if there is data before the array and then implement a for loop.
I will answer it rather then comment. You will get all the properties and their main image like this (place this in your controller action eg. index()):
$properties= $this->Property->find('all', array(
'contain' => array(
'Image' => array(
'conditions' => array(
'main_image' => true
)
)
)
));
$this->set('properties', $properties);
and in your view:
<img src='<?php echo '/realestateagencyadministration/img/' . $property['Image'][0]['main_image'] ?>' style='max-width: 100%;height:150px;;'>
Don't take my for sure on this last one, I am wrinting this out of my head, I think you need [0], but you can easily fix that.

Implementing pagination in cakephp

I wish to implement pagination in my search results in the view page. Currently in the controller, I limit it to 10 results to view. How do I implement pagination?
In my controller,
var $name = 'Searches';
var $components = array('Auth');
var $uses = array('User','Passion');
$users = $this->User->find('all',array('limit'=>10,'conditions'=>$final_conditions,'fields'=>array('User.*')));
In my view page,
<?php foreach ($search_fields as $user): ?>
<tr>
<?php //debug($search_fields);?>
<td><?php echo $user['User']['firstName']; ?></td>
<td><?php echo $user['User']['lastName']; ?></td>
<td><?php echo $user['User']['email']; ?></td>
<td><?php echo $user['User']['displayName']; ?></td>
<td><?php echo $user['User']['gender']; ?></td>
</tr>
<?php endforeach; ?>
Controller
$this->paginate = array(
'conditions' => $final_conditions,
'limit' => 10
);
$users = $this->paginate('User');
In the view you just use the same as on http://book.cakephp.org/view/166/Pagination-in-Views

Returning extra fields with generatetreelist

I don't know if I'm going the right way with the tree behavior but I'm trying to build a comment system for a blog. I would like to have an indentation of 5 level depth.
The generatetreelist method looks like it would be the fastest way to accomplish this but it doesn't look like you can add fields to the query. Am I right ? Is there a way to modify the method ?
Thanks
This is how I do it for categories. You could modify accordingly.
In controller:
$categories = $this->Category->generatetreelist(null,null,null,"|-- ");
$categories_array = array();
foreach($categories as $k => $v)
{
$categories_array[$k] = $this->Category->find('first', array('conditions' => array('Category.id' => $k)));
$categories_array[$k]["Category"]["path"] = $v;
}
$this->set(compact('categories','categories_array'));
In view:
<table>
<thead>
<tr>
<th><?php __('Id');?></th>
<th><?php __('Name');?></th>
<th><?php __('Status');?></th>
<th><?php __('Action');?></th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
foreach ($categories_array AS $categoryId => $category):
$class = 'even';
if ($i++ % 2 == 0) {
$class = 'odd';
}?>
<tr class="<?php echo $class;?>">
<td><?php echo $category['Category']['id']; ?></td>
<td><?php echo $category["Category"]["path"];?></td>
<?php if ($category['Category']['status'] == '1'){
$published = 'Active';
}else {
$published = 'Inactive';
} ?>
<td><?php echo $published;?></td>
<td>
<?php echo $html->link($html->image("icons/16_icon_view.png"), array('action' => 'view', $category['Category']['id']), array('title'=>'View','escape' => false));?>
<?php echo $html->link($html->image("icons/16_icon_edit.png"), array('action' => 'edit', $category['Category']['id']), array('title'=>'Edit','escape' => false));?>
<?php echo $html->link($html->image("icons/16_icon_delete.png"), array('action' => 'delete', $category['Category']['id']), array('class'=>'delete_trigger','rel'=>'#error','title'=>'Delete','escape' => false));?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
If it was me, I would just do this in the result set. As you get a numeric array in the first dimension of the results you could use that when outputting the data to indent or add a class to your comments as you needed.

Resources