symfony setDefault not saving values - doctrine

I'm defining a form in the actions, and passing it to the template. It's failing to set the default values when saving. Can you tell me what I'm doing wrong?
In modules\question\actions:
public function executeNew(sfWebRequest $request)
{
$this->form = new questionForm();
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
// create question
$user = $this->getUser()->getGuardUser()->getId();
$question = new Question();
$question->setQuestionTitle($this->getRequestParameter('question_title'));
$question->setQuestionBody($this->getRequestParameter('question_body'));
$question->setUserId($user);
$question->save();
$question->addTagsForUser($this->getRequestParameter('tag'), $user);
return $this->redirect('question/index');
}
//set current user as default
$this->form->setDefault('user_id',$this->getUser()->getGuardUser()->getId());
}
In \modules\question\templates:
<?php echo $form->renderFormTag('question/new') ?>
<table>
<?php echo $form['question_title']->renderRow(array('size' => 40), 'Question title:') ?>
<?php echo $form['question_body']->renderRow(array('cols' => 40, 'rows' => 10), 'Description:') ?>
<?php echo input_auto_complete_tag('tag', '', '#tag_autocomplete', 'autocomplete=off', 'use_style=true') ?>
<tr>
<td colspan="2">
<input type="submit" value="ask it" />
</td>
</tr>
</table>
</form>

One of two options:
Put the setDefault code in the QuestionForm class
OR
Put the setDefault code right after your instantiate the form object
The common practice is option #1, as it results in loose coupling between your form configuration and the actions.

Related

How to make sortable select multiply in laravel backpack?

I need to make sortable select2_multiple with Laravel Backpack - or a custom one with similar functionality.
In the docs there is a way to simply implement select2_multiple just like this:
$this->crud->addField([
'label' => "Ingredients",
'type' => 'select2_multiple',
'name' => 'ingredients', // the method that defines the relationship in your Model
'entity' => 'ingredients', // the method that defines the relationship in your Model
'attribute' => 'name', // foreign key attribute that is shown to user
'model' => Ingredient::class, // foreign key model
'pivot' => true,
'select_all' => true
]);
But! I dont see any way to store sort data of the entries we selected!
I have created field SORT in my DB ingredients_to_products
table, this should do the trick, but how to implement this to Backpack?
DB structure is really simple: products, ingredients, ingredients_to_products tables. Ingredients_to_products table have only 3 fields: product_id, ingredient_id, sort.
Laravel 5.6, Backpack CRUD 3.3
Any help is appreciated. Thanks!
I found how to do this.
I made a custom view and put some changes to CrudController, manually resolving sort value.
In view section I created some JS and new fields that represents sort values of relevant entries.
By the way, there is a $entry->pivot function that also can help.
So, what i've done: created new view called "ingredients"
<table class="table table-bordered" id="table">
<p>Connected ingredients:</p>
<select hidden multiple name="ingredients[]" id="ingredients">
<?php foreach ($field['value'] as $item){ ?>
<option selected data-id="<?= $item->id ?>" value="<?= $item->id ?>"><?= $item->name ?></option>
<?php } ?>
</select>
<select hidden multiple name="ingredients_sort[]" id="ingredients_sort">
<?php foreach ($field['value'] as $item){ ?>
<option data-id="<?= $item->id ?>" selected value="<?= $item->pivot->sort ?>"><?= $item->pivot->sort ?></option>
<?php } ?>
</select>
<?php foreach ($field['value'] as $item){ ?>
<tr class="item">
<td>
<p><?= $item->name ?></p>
</td>
<td>
<input class="sorter form-control" data-id="<?= $item->id ?>" value="<?= $item->pivot->sort ?>">
</td>
<td>
<span data-id="<?= $item->id ?>" class="btn btn-primary remover">Remove</span>
</td>
</tr>
<?php } ?>
So as you see we now have the ingredients and ingredients_sort arrays that syncronized by positions by JavaScript.
We get list of all avaliable ingredients by AJAX.
Sorting value - is a connected input field to every ingredient entry.
let selected_ingredients = <?= isset($field['value']) ? json_encode($field['value']) : '' ?>;
let ingredients = [];
document.addEventListener("DOMContentLoaded", load);
function load(){
getIngredients();
}
function getIngredients() {
$.get('/ingredients/get').done( (response) => {
ingredients = JSON.parse(response);
printList(ingredients);
} );
}
function printList(ingredients){
// #todo : print search
for (let i = 0; i < ingredients.length; i++){
let item = ingredients[i];
let _item = document.createElement('div');
_item.className = 'item btn btn-default';
_item.setAttribute('data-id', item.id);
_item.innerHTML = `${item.name}`;
_item.onclick = function () {
// #todo: add ingredient to product
let needAdd = true;
for (let i = 0; i < selected_ingredients.length; i++){
if (selected_ingredients[i].id == item.id){
needAdd = false;
}
}
if (needAdd){
$('#ingredients').append(`<option selected data-id="${item.id}" value="${item.id}">${item.name}</option>`);
$('#ingredients_sort').append(`<option data-id=${item.id} selected value="0">0</option>`);
$('#table').append(
`<tr><td>
<p>${item.name}</p>
</td>
<td>
<input class="sorter form-control" data-id="${item.id}" value="0">
</td>
<td>
<span data-id="${item.id}" class="remover btn btn-primary">remove</span>
</td>
</tr>`);
$('.remover').unbind().click(function () {
$(this).parent().parent().remove();
});
$('.sorter').unbind().change(function(){
let id = $(this).attr('data-id');
$(`#ingredients_sort [data-id=${id}]`).val( $(this).val() ).html( $(this).val() );
});
}
};
$('#list').append(_item);
}
$('.remover').unbind().click(function () {
let id = $(this).attr('data-id');
$(`#ingredients [data-id=${id}], #ingredients_sort [data-id=${id}]`).remove();
$(this).parent().parent().remove();
});
$('.sorter').unbind().change(function() {
let id = $(this).attr('data-id');
$(`#ingredients_sort [data-id=${id}]`).val( $(this).val() ).html( $(this).val() );
});
}
function remove(){
console.log(this);
}
And the last step - putting the Update method in ItemCrudController (which in my app is "product" controller)
public function update(UpdateRequest $request)
{
// your additional operations before save here
$redirect_location = parent::updateCrud($request);
$obj = \App\Models\Item::find( $request->get('id') );
$obj->ingredients()->detach();
$i = 0;
foreach ( $request->get('ingredients') as $item){
$obj->ingredients()->save( \App\Models\Ingredient::find($item), ['sort' => $request->get('ingredients_sort')[$i]]);
$i++;
}
return $redirect_location;
}
So - it works.
The result photo below. Hope this will help someone.

A PHP Error was encountered Severity: Notice Message: Undefined property: Site::$site_model

A PHP Error was encountered
Severity: Notice
Message: Undefined property: Site::$site_model
Filename: controllers/site.php
Line Number: 13
Site Controller site.php
Fatal error: Call to a member function delete_row() on a non-object in
C:\xampp\htdocs\login\application\controllers\site.php on line 13
My controller->Site.php
<?php
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
}
function delete(){
$this->site_model->delete_row();
$this->index();
}
function members_area(){
$this->load->model('patient_model');
$data['records'] = $this->patient_model->getAll();
$this->load->view('members_area', $data);
}
function add(){
$data['main_content'] = 'patient_form';
$this->load->view('includes/template',$data);
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true){
echo 'Your don\'t have permission to access this page. Login';
die();
}
}
}
My model -> site_model.php
class Site_model extends CI_Model{
function get_records(){
$query = $this->db->get('patient');
return $query->result();
}
function delete_row(){
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('patient');
}
function create_member(){
$new_member_insert_data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'address' => $this->input->post('address'),
'contact' => $this->input->post('contact'),
'remarks' => $this->input->post('remarks')
);
$insert = $this->db->insert('patient', $new_member_insert_data);
return $insert;
}
}
My view-> members_area.php
<?php $this->load->view('includes/header');?>
<div id="main">
Welcome Back, <u><?php echo $this->session->userdata('username'); ?>!</u>
<h3>Vision Eye Medical Center Patient Information</h3>
<div id="overflow">
<table class="features-table">
<thead>
<tr>
<td>Patient Name</td>
<td>Email</td>
<td>Address</td>
<td>Contact</td>
<td>Remarks</td>
<td>Action</td>
</tr>
</thead>
<tfoot>
<tr>
<td></td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
</tfoot>
<tbody>
<?php foreach($records as $row): ?>
<tr>
<td><?php echo $row->fname;?>
<?php echo $row->lname;?>
</td>
<td><?php echo $row->email;?></td>
<td><?php echo $row->address;?></td>
<td><?php echo $row->contact;?></td>
<td><?php echo $row->remarks;?></td>
<td>View|<?php echo anchor("site/delete/$row->id",'delete');?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php echo form_open();?>
<?php echo anchor('patient/add', 'Add new Data'); ?><?php echo anchor('login/logout', 'Logout'); ?><br>
<?php echo form_close();?>
</div>
<?php $this->load->view('includes/footer');?>
It seems you are not loading your "site model" anywhere in code you can do following:
autoload it in autoload file.
load your model either in constructor of controller, or load it whenever needed by using this line $this->load->model('site_model');
More information can be found here.
You need to load model in your controller.
class Site extends CI_Controller{
function __construct(){
parent::__construct();
$this->is_logged_in();
$this->load->model('site_model'); //Load site_model
........
In somecase it happens when you don't extract your form data to the model. Try extract($_POST) in your model and don't forget to load the model properly.
P.s.:this was the solution for my error

CakePHP Ajax Pagination

I am currently trying to get an ajax paginated list working. I think I'm close. When I click on the Previous, Next or individual page numbers, the correct results show but div.postsPaging is loaded with the paginated results along with Cake's default.ctp layout's header and footer. I've tried setting the layout to ajax in the displayPosts view or in the displayPosts action, but then the default layout doesn't appear at all (either inside or outside the div).
My question is, how can I load the postsPaging div with just the paginated results and not the header, footer or anything else in the layout. How can I get the results of the ajax request to not include the header and footer of the default layout?
The jQuery events attached to one of the page indexes and the Next button are:
$("#link-196981051").bind("click", function (event) {$.ajax({dataType:"html", success:function (data, textStatus) {$("#postsPaging").html(data);}, url:"\/AuthAdminCakeApp\/posts\/displayPosts\/page:2"});
return false;});`
$("#link-296431922").bind("click", function (event) {$.ajax({dataType:"html", success:function (data, textStatus) {$("#postsPaging").html(data);}, url:"\/AuthAdminCakeApp\/posts\/displayPosts\/page:2"});
return false;});`
My PostsController code looks like this:
class PostsController extends AppController {
public $helpers = array('Js' => 'Jquery');
public $paginate = array(
'limit' => 3,
'order' => array(
'Post.title' => 'asc'
)
);
public function displayPosts() {
$this->set('posts', $this->paginate());
}
// other actions
}
My paging element (paging.ctp) looks like this:
<?php
$this->Paginator->options(
array('update'=>'#postsPaging',
'url'=>array('controller'=>'posts',
'action'=>'displayPosts')));
?>
<table cellpadding="0" cellspacing="0">
<tr>
<th><?php echo $this->Paginator->sort('id'); ?></th>
<th><?php echo $this->Paginator->sort('user_id'); ?></th>
<th><?php echo $this->Paginator->sort('title'); ?></th>
<th><?php echo $this->Paginator->sort('body'); ?></th>
<th><?php echo $this->Paginator->sort('created'); ?></th>
<th><?php echo $this->Paginator->sort('modified'); ?></th>
<th class="actions"><?php echo __('Actions'); ?></th>
</tr>
<?php
foreach ($posts as $post): ?>
<tr>
<td><?php echo h($post['Post']['id']); ?> </td>
<td>
<?php echo $this->Html->link($post['User']['id'], array('controller' => 'users', 'action' => 'view', $post['User']['id'])); ?>
</td>
<td><?php echo h($post['Post']['title']); ?> </td>
<td><?php echo h($post['Post']['body']); ?> </td>
<td><?php echo h($post['Post']['created']); ?> </td>
<td><?php echo h($post['Post']['modified']); ?> </td>
<td class="actions">
<?php echo $this->Html->link(__('View'), array('action' => 'view', $post['Post']['id'])); ?>
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $post['Post']['id'])); ?>
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $post['Post']['id']), null, __('Are you sure you want to delete # %s?', $post['Post']['id'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
The view for the displayPosts action looks like this:
<div id="postsPaging">
<?php echo $this->element('Posts/paging'); ?>
</div>
Edit:
I'm using CakePHP 2.2.5.
you have to use an ajax layout.
$this->layout = ($this->request->is("ajax")) ? "ajax" : "default";
You can also place this code snippet in AppController, for Ajax responses application wide.
public function beforeRender() {
$this->layout = ($this->request->is("ajax")) ? "ajax" : "default";
}
Also make sure you are loading the RequestHandler component.
var $components = array('RequestHandler');
Putting the above in your Controller or AppController will handle setting the layout to ajax.

How to display Attribute Group Name on Product page?

I created Attribute Sets with more Grouped attributes inside. In admin, the custom attributes are displayed in groups (tabs) like I created them. But, on the product page, all the attributes are listed together, without displaying the Attribute Group Name before listing the Attributes in that Group.
How can I display also the Attribute Group names, not only the attributes? If you could show me on the default template, I will do it accordingly in my custom template.
Thank you!
Ok, I found an answer and I hope it will be useful to others in search for the same thing.
First, I'm using Magento 1.5.0.
Second, I found the answer in German here, with an extension already created, but the installation failed.
So, I added /app/code/local/Mage/Catalog/Block/Product/View/Attributesgroups.php with the following code:
<?php
class Mage_Catalog_Block_Product_View_Attributesgroups extends Mage_Core_Block_Template
{
protected $_product = null;
function getProduct()
{
if (!$this->_product) {
$this->_product = Mage::registry('product');
}
return $this->_product;
}
public function getAdditionalData(array $excludeAttr = array())
{
$data = array();
$product = $this->getProduct();
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
if ($attribute->getIsVisibleOnFront() && !in_array($attribute->getAttributeCode(), $excludeAttr)) {
$value = $attribute->getFrontend()->getValue($product);
// TODO this is temporary skipping eco taxes
if (is_string($value)) {
if (strlen($value) && $product->hasData($attribute->getAttributeCode())) {
if ($attribute->getFrontendInput() == 'price') {
$value = Mage::app()->getStore()->convertPrice($value,true);
} elseif (!$attribute->getIsHtmlAllowedOnFront()) {
$value = $this->htmlEscape($value);
}
$group = 0;
if( $tmp = $attribute->getData('attribute_group_id') ) {
$group = $tmp;
}
$data[$group]['items'][ $attribute->getAttributeCode()] = array(
'label' => $attribute->getFrontend()->getLabel(),
'value' => $value,
'code' => $attribute->getAttributeCode()
);
$data[$group]['attrid'] = $attribute->getId();
}
}
}
}
// Noch Titel lesen
foreach( $data AS $groupId => &$group ) {
$groupModel = Mage::getModel('eav/entity_attribute_group')->load( $groupId );
$group['title'] = $groupModel->getAttributeGroupName();
}
return $data;
}
}
Then, I created the /app/design/frontend/default/YOUR_TEMPLATE/template/catalog/product/view/attributesgroups.phtml file with the following content:
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct()
?>
<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<div class="box-collateral box-additional">
<h2><?php echo $this->__('Additional Information') ?></h2>
<?php $i=0; foreach ($_additionalgroup as $_additional): $i++; ?>
<h3><?php echo $this->__( $_additional['title'] )?></h3>
<table class="data-table" id="product-attribute-specs-table-<?php echo $i?>">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional['items'] as $_data): ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
<?php endforeach; ?>
</div>
<?php endif;?>
Last step was to modify /app/design/frontend/default/YOUR_TEMPLATE/layout/catalog.xml in line 223, and replaced
<block type="catalog/product_view_attributes" name="product.attributes" as="additional" template="catalog/product/view/attributes.phtml">
with
<block type="catalog/product_view_attributesgroups" name="product.attributes" as="additional" template="catalog/product/view/attributesgroups.phtml">
I repeat, this answer does NOT belong to me, I just translated what I found. Many thanks to the beautiful people who ended my three days of search with a clean and simple answer: WebGuys.DE
Also, thanks to #rpSetzer who cared to help!
Iterate through all the attributes and create an array of groups. In each group you put the attributes that belong to it. Then it's simple to display them the way you wanted.
Here is an implementation that is very close to what you need.
Thanks for translation : additionnal information for those who may need :
For 1st step, if the folder /app/code/local/Mage/Catalog/Block/Product/View/ doesn't exist, create it ! with correct persmissions and place the file Attributesgroups.php there
If you have different store views (for different languages), and you want to use correct translation for each attribute label, here is want you need to do :
In the file Attributesgroups.php
Replace 'label' => $attribute->getFrontend()->getLabel(), with'label' => $attribute->getStoreLabel(),

Insert in to database using codeigniter

HI
i want to insert data using form in CI but i am facing the problem
Here is my model code
function add_form() {
$this->load->database();
$id = $this->input->post('id');
$name = $this->input->post('name');
$age = $this->input->post('age');
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
$this->db->insert('user',$data);
}
Here is my controller code
function simpleform() {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('welcomedb_model');
if( $this->input->post('submit') ) {
$this->welcomedb_model->add_form();
}
$this->load->view('welcomedb_view');
}
and here is my view code
<?php echo form_open('welcomedb/submit'); ?>
<? echo $name; ?>:
<? echo form_input('name'); ?>
</br>
<? echo $age; ?>:
<? echo form_input('age'); ?>
</br>
<?php echo form_submit('submit', 'Submit'); ?>
<?php echo form_close(); ?>
Thanks for your help
Your form is submitting to welcomedb/submit, but your controller appears to be at welcomedb/simpleform... maybe you need to change that.
Otherwise, there doesn't appear to be anything wrong.
Probably:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
Remove comma from the last element (age) like this:
$data = array(
'name' => $this->input->post('name'),
'age' => $this->input->post('age')
);
and always dump error.
Best way is to do these code ......
First conifg the autoload and database.
into autoload:$autoload['libraries'] = array('database');
$autoload['helper'] = array('url','form','file');
Into controller
<?php
class CDemo extends CI_Controller
{
function index()
{
$this->load->view('VDemo');
}
function save()
{
$this->load->model('MDemo');
if($this->input->post('submit'))
{
$this->MDemo->process();
}
redirect('CDemo');
}
}
?>
Into Model
<?php
class MDemo extends CI_Model
{
function process()
{
$Id = $this->input->post('Id');
$Name = $this->input->post('Name');
$data = array(
'Id'=>$Id,
'Name'=>$Name
);
$this->db->insert('test',$data);
}
}
?>
Into View
<html>
<head>
<title>DEMO</title>
</head>
<body>
<h1>Form Biodata</h1>
<?php
echo form_open('CDemo/save', array('name' => 'VDemo'));
?>
<table>
<tr>
<td>Id :</td>
<td><input type="text" name="Id"></input></td>
</tr>
<tr>
<td>Name :</td>
<td><input type="text" name="Name"></input></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit"></input></td>
</tr>
</table>
<?php
echo form_close();
?>
<textarea rows="" cols="">Hello</textarea>
</body>
</html>

Resources