I'm very new to CakePHP (and only slightly less new to MVC concepts) and am trying to build a system with a HABTM relationship between events and participants. I'd like the index page to display a list of events in a table, with one cell of each row containing a full list of participants. So far, my table display loop code looks like:
<?php foreach ($events as $event): ?>
<tr>
<td><?php echo $event['Event']['id']; ?></td>
<td><?php echo $event['Event']['title']; ?></td>
<td><?php
foreach ($participants as $participant):
echo $participant['Participant']['name'];
endforeach;
?></td>
</tr>
<?php endforeach; ?>
I know this is wrong, but I've tried many variations on this theme (all of which seem to my mind as equally wrong), such as $event['Participant']... and nothing works. I know the answer is simple, but I don't know what it is and searching around just gives answers to similar, but not sufficiently similar, answers. What do I need to write?
I would do something like this considering recursive IS NOT 0 in your controller.
<?php foreach ($events as $event): ?>
<tr>
<td><?php echo $event['Event']['id']; ?></td>
<td><?php echo $event['Event']['title']; ?></td>
<td><?php
foreach ($event['Participant'] as $participant):
echo $participant['name'];
endforeach;
?></td>
</tr>
<?php endforeach; ?>
This is untested. So if you need anymore help on this solution, please let me know.
Related
There are two tables in our database that are important for this question. In Orders, we have the following fields:
ID, account_id, sale_id, site_id, pending, order_date
The other related table, orders_product_types, is an associative table that contains
order_id, product_id, quantity
Cake originally baked an admin page for orders that works just as you would expect. However, the client has requested that we add the ability to edit the quantity of each product associated with the sale on that same orders.admin_edit page, as well as see the products on the admin_view page. I have already managed to get the view page to display (albeit in an unpretty way) with the following code. To the controller, I added
$order = $this->Order->findById($id);
$this->recursive = 1;
to the controller and included it in the set, and I display them on the view page with this:
<?php foreach ($order['ProductType'] as $productType){
echo $productType['type'];
echo $productType['OrdersProductType']['quantity'];?>
<br></br>
<?
}?>
However, now I'm stuck on how to do this in the cake form for editing. Ideally, I would like to find a way to do this using proper cake functions, but I'm still new to this and am still figuring out how everything works. The pertinent part of the form that allows them to edit the information for the order looks like this:
<?php echo $this->Form->create('Order'); ?>
<fieldset>
<legend><?php echo __('Edit Order'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('account_id');
echo $this->Form->input('sale_id');
echo $this->Form->input('site_id');
echo $this->Form->input('pay later');
echo $this->Form->input('order_date');
echo $this->Form->input('Coupon');
echo $this->Form->input('ProductType');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
Ideally, I would like to find some way to follow this syntax, but to include fields from the associated table orders_product_types so that they can edit the quantities directly in this form. How would I include a second table within the cake structure?
I was unable to find a way to do it "properly" with cake syntax like I was looking for, but I did eventually solve the problem by inserting a table in the view as follows:
<table>
<? foreach($productTypes as $id => $productType):?>
<tr class="product">
<td><?= $productType ?></td>
<td class="price" id="<?= $id ?>"></td>
<td>
<select name="data[Quantity][<?=$id?>]" class="quantity">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
<td class="subtotal"></td>
</tr>
<? endforeach; ?>
<tr>
<td colspan="4" style="text-align: right" id="total"></td>
</tr>
</table>
If anyone has a proper way to do it, please let me know. I am still learning my way around cake, and every little bit helps.
On my Magento product page; when a product has multiple values for one custom attribute; instead of displaying the values it displays the text "array". It works fine with one value.
Thanks,
-Sam
You can do something like:
<?php
foreach($_product->getMetal() as $name => $value): ?>
<?php echo $name;?> = <?php echo $value;?>
<?php
endforeach; ?>
Magento takes advantage of PHP's magic getter/setter functionality (http://www.php.net/manual/en/language.oop5.overloading.php#object.get).
You can do a vardump($_product) to see the available attributes (they are stored in the _data array in the product). Then to retrieve one of them, you just remove the underscores and change the first letter of each word to uppercase.
EDIT:
If the above code doesn't output values, you can do this (which will tell you how to get to the value):
<?php
foreach($_product->getMetal() as $attribute): ?>
<?php var_dump($attribute); ?>
<?php
endforeach; ?>
I found this on Magento forums and it seems to work:
` getData('attribute_name')): ?>
getResource()->getAttribute('attribute_name')->getFrontend()->getValue($_product)) ?>
`
In my cake app I have a model called faqs, controller called faqs_controller & view called faqsindex.php.
I'm making a CMS so users can change the FAQs. The db table 'faqs' has 5 columns id, category, question, answer and number. "Number" is the order in which the FAQ's will appear.
The loop that lists all of the FAQs looks more or less like this:
<?php
foreach ($faqs as $faq):
<tr>
<td><?php echo $faq['Faq']['category']; ?></td>
<td><?php echo $faq['Faq']['number']; ?></td>
<td><?php echo $faq['Faq']['question']; ?></td>
<td><?php echo $faq['Faq']['answer']; ?></td>
</tr>
<?php endforeach; ?>
I want to make it so that the user can change the "number" cell from this screen, instead of going into a separate edit screen for each row and changing the number there.
You know, like how netflix's queue works, where the user can reorder it from the list, you don't have to click on the movie you want to see to change its order in your queue.
EDIT I set up edit in faqs_controller.php like this:
function edit() {
if(!empty($this->data)) {
$this->Faq->saveAll($this->data['Faq']);
}
else {
$this->data['Faq'] = Set::combine($this->Faq->find('all'), '{n}.Faq.id', '{n}.Faq');
}
}
and in the index view I made a foreach that looks like this:
echo $form->create('Faq', array('action'=>'edit'));
foreach($this->viewVars['faqs'] as $key => $value) {
echo 'id:'.$value['Faq']['id'];
echo '<br/>question:'.$value['Faq']['question'];
echo $form->input('Faq.'.$key.'.number');
}
In this case the foreach goes round 8 times because there are 8 rows. If I submit them, I create 8 new rows & can't update existing rows.
-EDIT-
I changed the form echo here:
echo $form->input('Faq.'.$key.'.question',array('value'=>$value['Faq']['question']));
to prepopulate the form. What I can't figure out is how to update the proper row. If I submit the form I get 8 mysql queries like this:
INSERT INTO faqs (question) VALUES ('THE NEW QUESTION I JUST ADDED?') when I don't want an insert but an update.
Put each faq id inside your form?
echo $form->create('Faq', array('action'=>'edit'));
foreach($this->viewVars['faqs'] as $key => $value) {
echo 'id:'.$value['Faq']['id'];
echo '<br/>question:'.$value['Faq']['question'];
echo $form->hidden('Faq.'.$key.'.id', array('value' => $value['Faq']['id']));
echo $form->input('Faq.'.$key.'.number');
}
if you are using jquery: http://jqueryui.com/demos/sortable/ All the ordering is done on client side, you put the order value into hidden input and use javascript to change them according to user interaction. There's nothing to change on the server side script.
Edit:
echo $form->create('Faq', array('action'=>'edit'));
foreach($this->data['Faq'] as $key => $value) {
echo 'id:'.$value['Faq']['id'];
echo '<br/>question:'.$value['Faq']['question'];
echo $form->input('Faq.'.$key.'.number');
echo $form->input('Faq.'.$key.'.id');
}
echo $form->end('Save');
and the controller:
function edit() {
if(!empty($this->data)) {
$this->Faq->saveAll($this->data['Faq']);
}
$this->data['Faq'] = Set::combine($this->Faq->find('all'), '{n}.Faq.id', '{n}.Faq');
}
unless you redirect them somewhere else after saving.
If you're wanting to update a specific row with Cake it is as simple as setting the appropriate row ID in your saveAll() query.
function edit() {
if (!empty($this->data)) {
$this->Faq->saveAll($this->data['Faq'], array('conditions' => array('Faq.id' => $yourId)));
}
...
}
This is telling Cake's ORM to save the information into the row only where Faq.id is equal to $yourId. If $yourId matches an existing ID in your table then that row should be updated.
Edit
You can put the id field into a hidden form element using CakePHP's FormHelper.
$this->Form->hidden('id', array('value' => $value['Faq']['id']));
I'm using containable to pull associated records into the view action and am getting an error message when looping through results.
Also, I'm using a 'sluggable' behaviour, so the find operation has a condition to search by this variable.
When I debug the find variable in the view, I do see the correct records. But when I try and loop through them in the view I get the 'Notice (8): Undefined index: error.' Ideally I liked to understand how to trouble shoot this error since it happens occasionally.
The model setup is:
tournaments have many tournamentDetails
tournamentDetails have many updates
The records I'm trying to display are:
tournament->tournamentDetails->updates
The tournament controller looks like this:
$tournament = $this->Tournament->find('first', array(
'conditions' => array( 'slug' => $slug),
'contain' => array(
'TournamentDetail' => array(
'Update' => array('order' => 'Update.id DESC'),
))));
The tournament view action looks like this:
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update ['Update']['date']; ?></h3>
<h4><?php echo $update['Update']['title']; ?></h4>
<p><?php echo $update ['Update']['body']; ?></p>
<hr/>
<?php endforeach; ?>
The 'update' data in the view when in debug looks like:
[Update] => Array
(
[0] => Array
(
[id] => 2
[title] => Indoor Challenge
[date] => 2010-03-19
[body] => Congratulations 2010 Champion
[tournament_detail_id] => 4
[homeStatus] => no
)
[1] => Array
(
[id] => 1
[title] => first round matches start today
[date] => 2010-03-19
[body] => this tournament's first round matches start today.
[tournament_detail_id] => 4
[homeStatus] => no
)
Is there something really obvious that I'm overlooking when looping through the 'updates' ?
Thanks, Paul
Resolution
Thanks everyone for the input. After taking a few steps back it occured to me what was happening with the 'undefined index' and comments about how to loop.
The problem was that the foreach loop wasn't nested, it only looped on the first level of association. The forearch loop deal with 'tournaments' which have many 'tournamentDetails'.
The loop needed to go one level deeper to 'tournamentDetails that have many 'updates'.
Here's the code that resolved this in the view.
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<?php foreach ($tournamentDetail ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
<?php endforeach; ?>
If others are looking to understand how to use the containable behavior with more than one level of association, just remember that you may have to have nested foreach loops in the view to display the results you're after.
Cheers, Paul
Paul,
given that the other things are right, could it be that you are mixing singular, plural, uppercase and lowercase? In particular the update/updates in various combinations.
Edit 0:
Quick shot?:
There simply is no such index defined, notice that you are working with an array consisting of arrays. It seems you have to use another loop nested in the first.
Edit 1:
Unfortunately, I can only provide quite generic help, as I do not sit infront of your code.
From your post and comments, I would guess that some variable names got mixed up. Just going over the whole dataflow normally does it. What is the exact variable name in the controller which delivers the correct data? If there is one, is it properly set from the controller to the view? Does the view address it correctly.
You are thinking great, this is what I did the last week, but I am quite sure that should do it. Clean out all the view code which currently makes use of any variables from the controller, proceed as described above keeping the dataflow in mind (but do not spend more than one hour on this).
If it does not work:
Something is foobared. Before you spend another week in despair, I would
try it without the containable behavior, see if it works
try to set up exactly the
same scenario in a completely new
CakePHP environment (in your case
1.2.5), see if it works
If you achieve your goals, try to see what went wrong in the original (often a face slapping moment).
If not:
try to see if there is a known bug
consider upgrading (or try to achieve your goals in 1.3.7 first)
Good luck, Benjamin
I think i understand you problem and please Try looping in this way
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
RSK's answer should be right, so since it's still not working you're probably not giving all the information needed. What does this output:
debug($tournament); ?
This all assumes your debug information is debug( $tournament ) and not debug( $update )
/* your old code */
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update ['Update']['date']; ?></h3>
<h4><?php echo $update['Update']['title']; ?></h4>
<p><?php echo $update ['Update']['body']; ?></p>
<hr/>
<?php endforeach; ?>
If the debug value you provided was a debug of the $tournament variable then the first part of your loop simply assigns the numerically keyed arrays inside of the tournaments variable to the $update value.
So, when you send $tournament[ 'Update' ] through the loop you are getting a structure in the $update array like the following.
array(
[id] => 2
[title] => Indoor Challenge
[date] => 2010-03-19
[body] => Congratulations 2010 Champion
[tournament_detail_id] => 4
[homeStatus] => no
)
But in your loop you are trying to access the keyed values as if they exist under an additional layer keyed with 'Update'. That key does not exist in your interior array.
So if my assumption is right - your loop should look like:
/* your old code with edits removing the additional interior Update key */
<?php foreach ($tournament ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
There also seemed to be some spacing in your brackets in the original code - I don't know if this is an issue or not but it looked odd to me.
Resolution
Thanks everyone for the input. After taking a few steps back it occurred to me what was happening with the 'undefined index' and comments about how to loop.
The problem was that the foreach loop wasn't nested, it only looped on the first level of association. The forearch loop deal with 'tournaments' which have many 'tournamentDetails'.
The loop needed to go one level deeper to 'tournamentDetails that have many 'updates'.
Here's the code that resolved this in the view.
<?php foreach ($tournament['TournamentDetail'] as $tournamentDetail): ?>
<?php foreach ($tournamentDetail ['Update'] as $update): ?>
<h3>Update: <?php echo $update['date']; ?></h3>
<h4><?php echo $update['title']; ?></h4>
<p><?php echo $update['body']; ?></p>
<hr/>
<?php endforeach; ?>
<?php endforeach; ?>
If others are looking to understand how to use the containable behavior with more than one level of association, just remember that you may have to have nested foreach loops in the view to display the results you're after.
Cheers, Paul
In my cakephp app I have an Option model.
In my option/index view I display 2 options with inputs and radio button fields.
I want to update both of them, but I get a weird behaviour.
The option I alter doesn't get saved and instead a new option is inserted with the new value.
Here is my view
<h2 class='page-title' id='manage-options'>Opzioni</h2>
<?php echo $form->create(null, array('action'=>'index')); ?>
<table>
<tr>
<td><?= $options[0]['Option']['name']?></td>
<td><?= $form->radio(
$options[0]['Option']['id'],
array(
'1' => 'Sì',
'0' => 'No'),
array('default'=> $options[0]['Option']['value'], 'legend'=>false)
);?>
</td>
</tr>
<tr>
<td><?= $options[1]['Option']['name']?></td>
<td><?= $form->input($options[1]['Option']['id'],array('label'=>false,'value' => $options[1]['Option']['value'] ))?></td>
</tr>
</table>
<?php echo $form->submit('Salva'); ?>
<?php echo $form->end(); ?>
And my controller:
function index() {
if (!empty($this->data)) {
foreach($this->data['Option'] as $id => $value) :
$this->Option->id = $id;
$feedback = $this->Option->read();
$this->Option->saveField('value', $value);
endforeach;
$this->Session->setFlash('Opzioni aggiornate');
}
$this->Option->recursive = 0;
$this->set('options', $this->paginate());
}
Before posting here I spent two hours googling for answers and experimenting. I know about saveAll() and i have tried these solutions:
http://planetcakephp.org/aggregator/items/2172-cakephp-multi-record-forms
http://teknoid.wordpress.com/2008/10/27/editing-multiple-records-with-saveall/
I have been tweaking my code to fit these patterns, but I got no results (oscillating between 'not working' and 'not working and I get an extra record'), so I decided to post my original code.
Can you help, indicating the most proper way to do this?
Cheeers,
Davide
The problem was with the data in the DB. The kind folks on the cakephp IRC channel called my attention to the fact that in most databases ID = 0 equals 'new record'. For some reason I had an option with ID 0, so when updating the underlaying mysql query actually created a new record.
Changed the IDs, problem fixed.
The main problem with your code that I see is that your fields, both the radio and the input, are being built with only an ID value as the first parameter. The correct "cake way" of building a field is having the first parameter be Model.fieldname, in your case I believe it would be $form->input('Option.id', array())?>
If you inspect the html generated by your code you will see the field name is data[id], and it should be data[Option][id] if you want to loop through $this->data['Option'] in your controller.
Try changing your code to include the Model.fieldname as the first parameter and then it should have the data submitted correctly to your controller.