created a view function and any time i click the link to view a template, the url at the top of the page is correct but it spits out the same list of fields in the database.
the fields are
accounts - id, company name, abn
template - id, name, description, account_id
field - id, name, field type, template_id
function view(){
$accounts=$this->User->AccountsUser->find('list',
array('fields'=>array('id', 'account_id'),
'conditions' =>array('user_id' =>
$this->Auth->user('id'))));
$templates=$this->Template->find('first',
array('conditions' => array(
'Template.account_id' => $accounts)));
$fields=$this->Field->find('all',
array('conditions' => array(
'Field.template_id' => Set::extract('/Template/id', $templates))));
$this->set('template', $templates);
$this->set('account', $accounts);
$this->set('field', $fields);
}
here is the view
<div class = "conlinks">
</br></br></br></br></br><h2>Here is your template fields</h2></br>
<?php foreach($field as $fields): ?>
<tr>
<td align='center'><?php echo $fields['Field']['name']; ?>
</tr></br>
<?php endforeach; ?>
</div>
so the problem is its grabbing the exact same list of fields, not the correct template_id when it prints out the fields
You should be able to debug this for yourself. Just narrow the bug down step by step.
For starters, in your view function, do a print_r on the following variables, and make sure each one contains a logical result:
$accounts
$templates
$fields
If you find unexpected results there, I'd be looking at the parameters you pass into each of your finds, and making sure they're OK. You're passing in $accounts as an array to your find condition - make sure it matches the format that cake expects. Do the same for Set::extract('/Template/id', $templates).
Also look at the SQL that Cake is producing.
If you're not already using it, I'd highly recommend installing Cake's Debug Kit Toolbar - https://github.com/cakephp/debug_kit/ because it makes debugging variables and SQL much easier.
If you do the above steps and can't solve your problem, you should at least be able to narrow it down to a line or two of code. Update your answer to show what line or two is causing the problem, and include print_r's of some of the variables you're working with. That should help others on StackOverflow to give you a specific answer.
Hope that helps!
the issue was I wasn't getting the parameters when click the link
function view($name){
$fields = $this->Template->Field->find('list',array(
'fields'=> array('name'),
'conditions' => array(
'template_id'=> $name)));
$this->set('field', $fields);
}
and the view
<div class = "conlinks">
</br><h2>Here is your template fields</h2>
<?php foreach($field as $name): ?>
<tr>
<td align='center'>
<?php echo $name; ?>
</tr></br>
<?php endforeach; ?>
</br>
<?php
echo $this->Html->link('Back', '/templates/view', array('class' => 'button'));?>
</div>
Related
I just started using CakePHP, version 3. What I liked to start with is creating a menu based on a database table.
For creating a menu, I have seen that it should be placed inside the Element folder and called in 'default.ctp'. No problems here.
The problem is: How can I retrieve the data from the table while I'm not working in the controller, using the power of CakePHP? I can't find an example or something.
Inside 'Element/Menus/main.ctp' I do something like this (I know the link is not correct):
<?php
$menus = $this->requestAction('/Menus/index');
foreach ($menus as $menu) {
?>
<li><?= $this->Html->link('Menu', ['controller' => 'Menus', 'action' => 'index', '_full' => true]); ?></li>
<?php
}
?>
I try to get the data by calling 'requestAction' but it doesn't seem to be the right way.
I'm inserting an image in my template file as
<?php echo $this->Html->image('profile_picture.png', array('alt'=>'frame partner avatar'), array('id'=>'framePartnerAvatar')); ?>
Now I'm trying to give style to this image by using the id mentioned, but on checking through Firebug only source and alternate name is visible. Image id is not coming there.
You have to use the correct syntax:
<?php echo $this->Html->image(
'profile_picture.png',
array(
'alt'=>'frame partner avatar',
'id'=>'framePartnerAvatar'
)); ?>
See HtmlHelper::image()
According to the documentation the method used to create images requires two parameters. The first one being the source link and the second one being the options parameter
$this->Html->image('cake_logo.png', ['alt' => 'CakePHP']);
Your code should be something along the lines of this
$this->Html->image('profile_picture.png', array(
'alt'=>'frame partner avatar',
'id'=>'framePartnerAvatar')
);
$thumb_img = $this->Html->image('yourimage.png',array('alt'=>'yoursite.com','id'=>'yourId'));
Please ensure that all HTML attributes such as alt, id, class etc needs to be written within a single array, and not multiple arrays.
<?php echo $this->Html->image('profile_picture.png', array(
'alt'=>'frame partner avatar',
'id'=>'framePartnerAvatar'
)
); ?>
This will certainly generate the following HTML:
<img src="/img/profile_picture.png" alt='frame partner avatar' id='framePartnerAvatar' />
Peace! xD
I want to echo a full name from my MySQL database in my header. When that name is clicked in a list it filters all the records and displays all the records related to that name only. I managed to get the filter working, but not able to display the name in header.
<? $this->read('$jobs as $row'); ?>
<h1><?=$row['Employee']['first_name']?> <?=$row['Employee']['last_name']?>'s Jobs</h1>
<? $this->end(); ?>
If I'm not wrong, you are trying to retrieve this array, I'm assuing $jobs contains single row.
try this
<?php
if (isset($jobs)) {
foreach($jobs as $row){
if (isset($row['Employee']['last_name']))
$last = $row['Employee']['last_name'];
$first = 'N/A';
if (isset($row['Employee']['first_name']))
$first = $row['Employee']['first_name'];
?>
<h1><?php echo $first.' '. $last?>'s Jobs</h1>
<?php } }?>
OR
<h1><?php isset($jobs[0]['Employee']['first_name']) ? $jobs[0]['Employee']['first_name'] : 'N/A' .' '. isset($jobs[0]['Employee']['last_name']) ? $jobs[0]['Employee']['last_name'] : 'N/A'?>'s Jobs</h1>
This can be much more easily achieved through the use of virtual fields. The example in the Cake book is practically identical to your needs.
Just add this to your Employee model:
public $virtualFields = array(
'full_name' => 'CONCAT(Employee.first_name, " ", Employee.last_name)'
);
Now [Employee]['full_name'] can be used without having to use any logic.
Here's the link to the Cake book page covering virtual fields: http://book.cakephp.org/2.0/en/models/virtual-fields.html
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.