Get result from left join in fuelphp - fuelphp

I has a table name "Account". It has three columns named "Id", "Name", "primary_user_id".
I want to show id, name and name corresponding to user id. I mean I have t use left join.
I get collection using
$data['accounts'] = Model_Account::find('first', array(
'related' => array(
'accounts' => array(
'join_type' => 'inner',
'where' => array(
array('primary_user_id', 'id')
),
'order_by' => array('id' => 'desc'),
),
),
));
In model:-
protected static $_has_many =array('accounts');
In view:-
<?php foreach ($accounts as $account): // echo "<pre>"; print_r($users);?>
<tr>
<td><?php echo $account->id; ?></td>
<td><?php echo $account->name; ?></td>
<td></td>
</tr>
<?php endforeach; ?>
I am trying to get value of name corresponding to primary_user_id. Please help me how can I achieve it.

If you are using the ORM you should not be manually constructing your queries like that. You will want to set up a relation between your Account and User models. There is plenty of documentation about relations in the main fuelphp docs.

Related

CakePHP 3.x - Creating/updating data without forms

I recently read this post on how to do a CakePHP 3.x POST without forms (doing updates on the Index.ctp page, rather than using Add.ctp or Edit.ctp).
I have 2 issues:
Modifying POST to accept select statements from an array?
My index.ctp is as follows:
<?php foreach ($resources as $resource): ?>
<tr>
<td><?= $this->Number->format($resource->id) ?></td>
<td><?= h($resource->brand) ?></td>
<td><?= h($resource->model) ?></td>
<td><?= h($resource->subtype) ?></td>
<td><?= $this->Form->postButton('Change Type',['controller'=>'Users', 'action'=>'change_resource', $resource->id, $resource->type->name])?></td>
<td><?= $resource->has('studio') ? $this->Html->link($resource->studio->name, ['controller' => 'Studios', 'action' => 'view', $resource->studio->id]) : '' ?></td>
<td><?= h($resource->created) ?></td>
<td><?= h($resource->modified) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $resource->id]) ?>
<?= $this->Html->link(__('Edit'), ['action' => 'edit', $resource->id]) ?>
<?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $resource->id], ['confirm' => __('Are you sure you want to delete # {0}?', $resource->id)]) ?>
</td>
</tr>
<?php endforeach; ?>
And then the action as described in the linked StackOverflow post in the corresponding Controller:
public function change_resource($id,$existing_type)
{
$resources = TableRegistry::get('Resources');
$resource = $resources->get($id);
$resource->type = ($existing_type == '')?'$resource->$type->name';
$resources->save($resource);
return $this->redirect($this->referer());
}
Currently I'm getting a syntax error of
unexpected ';' on line 124,
which refers to the $resource->type line in the Controller method. If I remove that line, I get:
unexpected $resources on line 125
which I expect given the ; is removed so it thinks they're all on the same line.
Modifying POST for non-select inputs (eg. varchar/int/text)
I would like to be able to do a POST for the entire table, not just one input. However, while 2 of the inputs are selects from other tables in the form of arrays, the rest are all standard inputs (mostly varchar). I'm wondering how to modify the above controller to do this. Additionally, if I want to do POST for more than one input, do I require separate methods for every single input, or can I put it all in one method?
Please note that your ternary operator syntax is incorrect. You're missing out the colon (:)
Refer to Ternary Operator Example
Syntax: (expr1) ? (expr2) : (expr3)
$resource->type = ($existing_type == '') ? $resource->$type->name : <other statement>;

How do I create WooCommerce Custom Product Sorting style?

I want to change the default sorting style given in WooCommerce plugin.
Example
I suppose you have already copy the WooCommerce template woocommerce/templates to your theme. You can find the WooCommerce structure here.
Once you have the WooCommerce template under your theme, looking for woocommerce/loop/orderby.php, this is where you can custom the product order. To change different order list, here is the code snippet to demonstrate how to do that:
//your-theme/woocommerce/loop/orderby.php
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters('woocommerce_catalog_orderby', array(
'title' => __('Alphabetically', 'woocommerce'),
'date' => __('Most Recent', 'woocommerce'),
'price' => __('Price Asc', 'woocommerce'),
'price-desc' => __('Price Desc', 'woocommerce')
));
?>
<?php foreach ( $catalog_orderby as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>><?php echo esc_html( $name ); ?></option>
<?php endforeach; ?>
</select>
</form>
You can add or remove any options from the $catalog_orderby above.
**'price-asc**' => __('Price Asc', 'woocommerce'),
'price-desc' => __('Price Desc', 'woocommerce')
));
Must changed to this now work*

ZEND - decorators and position of elements?

I've got a problem with zend decorators and can't figure out how to place an element in form where i want to have him.
I got a form where decorators are putting my data into a table, but i want to have my submit out of the table. For now i have something like that:
for ($i = 0; $i < $numRows; $i++) {
$select = new Zend_Form_Element_Select('article' . $i);
$select->setAttrib('style', 'margin-top:10px;width:200px;');
$select->addMultiOption('1', $this->translate->_('Zatwierdzony do druku'));
$select->addMultiOption('0', $this->translate->_('Niezatwierdzony do druku'));
$select->setValue($rows[$i]['reviewed']);
$select->setDecorators(
array(
'ViewHelper',
array(
array('data' => 'HtmlTag'),
array('tag' => 'td', 'class' => 'padding5 tdcenter')
),
array(
'AnyMarkup',
array(
'markup' => $this->_getMarkupForRow($i, $rows),
'placement' => 'PREPEND',
)
),
array(
array('row' => 'HtmlTag'),
array('tag' => 'tr', 'openOnly'=>true)
),
),
array('submit'),
false
);
$this->addElement($select);
}
$submit = new Zend_Form_Element_Submit('updatetoprint');
$submit->setAttrib('id', 'updatetoprint');
$submit->setLabel('Zapisz');
$submit->setDecorators(
array(
'ViewHelper',
array(
array('data' => 'HtmlTag')
),
array(
array('row' => 'HtmlTag'),
array('tag' => 'div', 'closeOnly'=>true,'style' => 'float:left;')
)
//here i dont know how to get my submit on the bottom under the table...?
),
array('submit')
);
$this->addElement($submit);
On site that looks like this:
And i'd like to have my submit on the bottom under the table... Please help me :)
I don't see where you are assigning your form to the view or if your form is a Zend_Form or not so I'm going to make some assumptions.
I'm going to assume you are using Zend_Form to build your form and I'm going to assume you are assigning your form to the view in the traditional manner inside a controller action $this->view->form = $form;
So with that being said, in your view script you have access to each individual element simply by <?php echo $this->form->elemenetName ?> where elementName is the name you have given your element in your case your submit button has the name 'updatetoprint'.
With this in mind if you need to create a table or any other layout you could simply create generic elements in your form and just add them as needed to any view script. Might prevent you from performing all those gymnastics with decorators.
For example view.phtml might look like:
<form action="<?php echo $this->form->getAction() ?>" method="<?php echo $this->form->getMethod() ?>">
<table>
<tr>
<th>Dodal</th>
<th>Tutyl</th>
<th>Status</th>
</tr>
<!--you should probably put the next 5 lines into a partial and use partialLoop($data)-->
<tr>
<td><?php echo $this->form->dodal ?></td>
<td><?php echo $this->form->tutyl ?></td>
<td><?php echo $this->form->status ?></td>
</tr>
</table>
<?php echo $this->form->updatetoprint ?>
</form>
Then just style with css.
To setup the form to use the viewscript just use:
$form->setDecorators(array(
'PrepareElements',
array('ViewScript', array('viewScript' => 'form.phtml')),
));
where form.phtml is your viewscript name
[Edit]
After further consideration I realized that diplaying form elements in this manner has one serious drawback. The form is never initialized...the form tags are never sent. This edit is to correct that issue.
Sorry it took so long
I'd suggest adding all your elements apart from the submit button to a display group. You then give the display group a HtmlTag decorator (with the tag set to table), which will wrap all your elements with a table, making your HTML valid and giving you the layout you want.
I'm seeing lots of <tr> and <td> tags, but no <table> tag. What I think you are looking for is markup roughly like this:
<table>
<tr><td>info</td><td>more info</td><td>select element</td></tr>
<tr><td>info</td><td>more info</td><td>select element</td></tr>
<tr><td>info</td><td>more info</td><td>select element</td></tr>
</table>
<div>
<input type="submit">
</div>
What seems to be missing from your code are any decorators that produce the <table> and </table> tags. So, on the first iteration of the loop, use the AnyMarkup decorator to prepend a <table> and your header row. On the last iteration, add a decorator that appends the </table> tag.
Then your submit button (which is added last) should sit right underneath.

zend paginator make other links and Action Calling concatinating with the URL

I am NEW to ZF .i used zend paginator in my first project .its working fine that is switching b/w pages with right result but the problem is that i have other links too in that view have a look to my view
<?php include "header.phtml"; ?>
<h1><?php echo $this->escape($this->title);?></h1>
<h2><?php echo $this->escape($this->description);?></h2>
Register
<table border="1" align="center">
<tr>
<th>User Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
</tr>
<?php
foreach($this->paginator as $record){?>
<tr>
<td><?php echo $record->user_name;?></td>
<td><?php echo $record->first_name;?></td>
<td><?php echo $record->last_name;?></td>
<td>
Edit
|
Delete
</td>
</tr>
<?php } ?>
</table>
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>
<?php include "footer.phtml"; ?>
as i said the pagination renders and working fine but when i click on these links
<a id="edit_link" href="edit/id/<?php echo $record->id;?>">Edit</a>
or
<a id="delete_link" href="del/id/<?php echo $record->id;?>">Delete</a>
or
Register
it is not calling the required action instead it make my url like this
(initial link) http://localhost/zend_login/web_root/index.php/task/list
after clicking any of the above link its like this
http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/8
http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/23
http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/register http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/del/id/12
note its not happening when the page renders first time but when i click on any pagination link its doing so initialy its going to the reguired action and displaying a view...any help HERE IS THE ACTION
public function listAction(){
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$sql = "SELECT * FROM task ORDER BY task_name ASC";
$result = $DB->fetchAll($sql);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(3);
$paginator->setCurrentPageNumber($page);
$this->view->assign('title','Task List');
$this->view->assign('description','Below, are the Task:');
$this->view->paginator=$paginator;
}
Try:
// controller
$this->view->controllerName = $this->getRequest()->getControllerName();
// view script
Edit
|
Delete
or
Edit
|
Delete
Second example uses baseUrl() view helper that's using front controller's baseUrl setting. If you don't set baseUrl in your frontController it's trying to guess. As you're not using bootstrap functionality to set baseUrl you may do the following in index.php (not required):
$frontController = Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/');
Third possibility using url() view helper:
<a href="<?php echo $this->url(array(
'controller' => $controllerName,
'action' => 'edit',
'id' => $record_->id
)); ?>">Edit</a>
|
<a href="<?php echo $this->url(array(
'controller' => $controllerName,
'action' => 'del',
'id' => $record_->id
));?>">Delete</a>
add this in your action
$request = $this->getRequest();
$this->view->assign('url', $request->getBaseURL());
and replace your links in view with this
Add a Task
Edit
Delete

Using ViewScript Decorator on Nested Subforms (Zend Form)

I want to use a view script to render my zend form as it seems to be the best way to
control the layout/design of the form while still using the Zend_Elements classes.
From the view script, I render the element with $this->element->getElement('elementName') .
I'm having problems with the names of the elements. This is actually a sub-form inside a sub-form inside a form.
When I used the FormElements decorators , the fully qualified name of the elements was form[subForm][subForm][element] , which was good.
Wehn I moved to the viewScript decorators, it changed to subForm[subForm][element].
I understood that I need to use the PrepareElements decorator to fix this, but this caused the name to change form[subForm][form][subForm][subForm][elements] (it doubled the first two names in the start).
Any ideas how I should handle this?
Thanks.
UPDATE: I tried to debug PrepareElements and I really don't understand what is doing.
It seems like it works ok in the first iteration, but then it adds again the form[subform] prefix when running on one of the middle subforms.
When I'm not using the PrepareElements decorator, I'm just missing the "form" prefix in the names (i.e., instead of form[subForm][element], I get only subForm[element]).
May be I can just fix this somehow?
I tried to change the belongsTo but that only replaced the "subForm" prefix .
It actually seems like what is missing is a belongsTo method on the subForm.
Again, this is all because of the ViewScript decorator. It works fine with the FormElements decorators.
UPDATE 2: Just to clarify, I wouldn't mind this name change, but it causes my fields to not populate when I call form->populate .
Edit: I think that I've narrowed the problem to this: when I get my values back in setDefaults, they are ordered like this:
array(
\"formElements1-name\" => value1... \"subFormName\" => array(
\"parentFormName\" => array(
\"subFormName\" => subForm-values-array
)
)
...
The main problem here is the "parentFormName" => "subFormNAme".. what does it repeat itself? I'm already in the main form. I'm guessing this is caused because I've set the setElementsBelongTo(formName[subFormName]) , but if I wouldn't do that, then I would get my subform values completely separate from the form,
i.e.
values array = array(
\"formName\" => array(
formValues
), \"subFormNAme\" => array(
subFormValues
)
, while I exepct it to be
array(
formName => array(
subFormNAme => values-array
)
)...
Is it even possible to make this work?
Are you just trying to output your form using <?php echo $this->form; ?> from your view script?
That works well for simple forms, but for my more complex forms I tend to render each element individually but don't need to use ViewScript decorator on each individual element to do this. Just try something like this from your view script:
<div class="form">
<fieldset>
<legend>Some Form Name</legend>
<form action="<?php echo $this->escape($this->form->getAction()) ?>"
method="<?php echo $this->escape($this->form->getMethod()) ?>"
enctype="multipart/form-data">
<?php echo $this->form->id; // render the id element here ?>
<div class="half">
<?php echo $this->form->name; // render the user name field here ?>
</div>
<div class="half">
<?php echo $this->form->description; // render the description element here ?>
</div>
<div class="clear"></div>
<div class="half">
<?php echo $this->form->address1; // render the address ?>
</div>
<div class="half">
<?php echo $this->form->address2; // render address2 ?>
</div>
<div class="clear"></div>
<div class="third">
<?php echo $this->form->zip; // render zip code ?>
</div>
<div class="third">
<?php echo $this->form->city; // render city ?>
</div>
<div class="third">
<?php echo $this->form->state; // render state ?>
</div>
<div class="clear"></div>
<div class="half">
<?php echo $this->form->country; // render country ?>
</div>
<div class="clear"></div>
<?php echo $this->form->submit; ?>
</form>
</fieldset>
</div>
That is how I do most of my forms because I want to have some elements take up half the width and others the full width.
Surprisingly, the reference guide doesn't tell you that you can do this. I seem to remember a page about it in the past but cannot find it now. When I got started with Zend Framework, I thought the only way I could get my form to output exactly how I wanted was to create complex decorators, but that is not the case.
Matthew Weier O'Phinney has a great blog post on rendering Zend_Form decorators individually which explains what I did above. I hope they add this to the first page of Zend Form because that was discouraging to me at first. The fact is, 90% of my forms render elements individually instead of just echo'ing the form itself.
Note: To stop ZF from enclosing my form elements in the dt and dd tags, I apply this decorator to all of my standard form elements. I have a base form class that I extend all of my forms from so I don't have to repeat this everywhere. This is the decorator for the element so I can use tags to enclose my elements.
public $elementDecorators = array(
'ViewHelper',
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
array('HtmlTag', array('tag' => 'div', 'class' => 'form-div')),
array('Label', array('class' => 'form-label', 'requiredSuffix' => '*'))
);
For my submit buttons I use
public $buttonDecorators = array(
'ViewHelper',
array('HtmlTag', array('tag' => 'div', 'class' => 'form-button'))
);
The current solution is to use the PrepareElements decorator on the subforms with one change - remove the recursive call in the PrepareElements code. Also, no "setElementsBelongTo" is required.
This seem to generate the correct names and ids.
The solution would be to use the belongsTo() form element property.
Example :
new Zend_Form_Element_Text('<elementName>', array('belongsTo' => '<subformName>'))
In this way, the render() method will use a form element name like
name="<subformName>[<elementName>]"
I had the same problem and i solved it with a decorator
1 : Create a generic subform with elements
2 : Using a specific decorator with PrepareElements
3 : Change form to an array with setIsArray(true)
Example :
Form
$i = 4;
for($i = 0; $i < $nbReclam ; $i++)
{
$rowForm = new Zend_Form_SubForm($i);
$name= new Zend_Form_Element_Textarea('name');
$rowForm->addElement($name);
$this->addSubForm($rowForm, $i);
}
$this->setDecorators(array(
'PrepareElements',
array('ViewScript', array('viewScript' => 'myDecorator.phtml')),
));
$this->setIsArray(true);
Decorator
<table>
<thead>
<tr>
<th>N°</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->element->getSubForms() as $subForm) : ?>
<tr>
<td> <?php echo $i++?> </td>
<?php foreach ($subForm->getElements() as $row) : ?>
<td><?php echo $row ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
Enjoy
Sorry for my english, i am french