ZF2 - inputFilter -> set value to null - mysql

In my ZF2 app I've got a form with several optional fields. If user decide to leave that field blank then it should set it as NULL in db, however - it doesn't work.
Example field:
$this->add(array(
'name' => 'productId',
'type' => 'select',
'options' => array(
'value_options' => array(
'global' => 'Global option', // this should be null
'mobile' => 'Another option'
)
),
));
And filter:
$inputFilter->add(array(
'name' => 'productId',
'validators' => array(
array(
'name' => 'Callback',
'options' => array(
// 'messages' => array(
// \Zend\Validator\Callback::INVALID_VALUE => $value,
// ),
'callback' => function ($value) {
if($value === 'global')
{
$value = null;
//return true;
// echo '<pre>' . var_export($value, true) . '</pre>';
// die();
}
}
),
),
),
));
This piece of code works when "mobile" option is selected. However, with 'global' option it doesn't work.
As you can see, I've done some basic debugging to make sure that value is overrided on callback and it indeed returns NULL. Regardless of that validator says:
array (
'productId' =>
array (
'callbackValue' => 'The input is not valid',
),
)
So I tried to return true on callback which resulted in below error:
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (app.codes, CONSTRAINT fk_products_id FOREIGN KEY (productId) REFERENCES products (id)))
How am I supposed to pass null param to my db? If I cut this field completly from form then it is ignored and everything works.

Found solution, it's pretty simple actually.
I can set desired select options to some specific value and then use ToNull filter to convert it into null -
https://framework.zend.com/manual/2.4/en/modules/zend.filter.null.html
Code for $inputFilter:
$inputFilter->add(array(
'name' => 'productId',
'required' => false,
'filters' => array(
array('name' => 'ToNull', 'options' => array('type' => \Zend\Filter\ToNull::TYPE_STRING)),
),
));

Related

Yii2 update with multiple conditions

$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId', $this->user,
'product_id', $this->productId,
'name', $el['old'],
'created', $el['date'],
'category', $el['cat'],
]
);
I tried to use the following command to update a row using multiple where conditions, but it doesn't work for some reason. The documentation doesn't seem to cover this case and doesn't seem to be updated, because the update() method seems to match the updateAll method instead of the update method for some reason (maybe it was updated?). So I was wondering what was the correct way to do this.
You have a syntax error. Try following:
$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat'],
]
);
Try This updateAll query :
MYTable::updateAll([ // field to be updated in first array
'name' => $el['new'],
'data' => $el['data']
],
[ // conditions in second array
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat']
]);

Call to undefined method Varien_Db_Statement_Pdo_Mysql::addColumn()

When i try to run my upgrade script it gives Call to undefined method Varien_Db_Statement_Pdo_Mysql::addColumn() error on second addcolumn but when i remove all other column and keep only one addColumn it work fine.My upgrade script is as below
$installer->startSetup();
/**
* alter table 'savecart/savecart'
*/
$installer->getConnection()
->addColumn($installer->getTable('savecart/savecart'),'savecart_name', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'length' => 255,
'comment' => 'Savecart Name'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_comment', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Savecart Comment'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_bill_id', array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
'nullable' => true,
'length' => 10,
'comment' => 'Billing Id'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_valid_till', array(
'type' => Varien_Db_Ddl_Table::TYPE_DATE,
'nullable' => true,
'comment' => 'Valid Till Date'
));
$installer->endSetup();
CHAINING CORRECTLY
I was looking into this and found several sites that use a different writing method:
http://magedevguide.com/challenge/chapter4/1
http://magento.ikantam.com/qa/how-setup-magento-scripts
Please try these methods with your values..
You can try copy pasting following code, but please review it for your values..
$installer->startSetup();
/**
* alter table 'savecart/savecart'
*/
$installer->getConnection()
->newTable($installer->getTable('savecart/savecart'))
->addColumn('savecart_name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array('nullable' => true), 'Savecart Name')
->addColumn('savecart_comment', Varien_Db_Ddl_Table::TYPE_TEXT, null, array('nullable' => true), 'Savecart Comment')
->addColumn('savecart_bill_id', Varien_Db_Ddl_Table::TYPE_INTEGER, 10, array('nullable' => true), 'Billing Id')
->addColumn('savecart_valid_till', Varien_Db_Ddl_Table::TYPE_DATE, null, array('nullable' => true), 'Valid Till Date');
$installer->endSetup();
Please let me know if one of the solutions is working so I can update my answer.
Please use following upgrade script add multiple columns to the existing magento table.for detailed explanation.
https://www.pearlbells.co.uk/upgrade-script-multiple-columns-magento/
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'filters_workshop_colour',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Filters Workshop Colour'
));
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'splashbackcolor',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Splash Back Color'
));
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'cabinetcolor',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Cabinet Color'
));

Datatables : Make a parallel request in another table using ServerSide

EDIT : I found the solution by replacing the SSP class by a customized SSP class I've found here : https://github.com/emran/ssp
I don't know if it's an understandable title, but here is my problem :
I have a DB-table (called projects) that needs to be inserted in a datatable. I've no problem to make a call using ServerSide and get results in the datatable.
But, for each project (each row), there is a project creator (column creator_id in the projects DB-table). What I need to do is to make a request to the creators DB-table in order to get the firstname/lastname of the creator, each time I get a row from the projects DB-table. Is that make sense?
Here is the code I use :
$table = 'projects';
$primaryKey = 'project_id';
$columns = array(
array(
'db' => 'project_id',
'dt' => 'DT_RowId',
'formatter' => function( $d, $row ) {
return $d;
}
),
array( 'db' => 'creator_id',
'dt' => 'creator',
'formatter' => function( $d, $row ) {
// Here I need to make a call to the creators DB-table and return the $creator_name value
return $creator_name;
}
)
);
// SQL server connection information
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require(BASE_DIR.'/lib/dataTables/ssp.class.php');
$result = SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns);
You can use formatter property to make a separate SQL query but it will affect performance greatly by increasing script response time.
Instead you can use the following trick when using ssp.class.php with sub-queries instead of table name to use GROUP BY, JOIN, etc.
<?php
$table = <<<EOT
(
SELECT projects.project_id, creators.creator_id, creators.creator_name
FROM projects
LEFT JOIN creators ON projects.creator_id=creators.creator_id
) t
EOT;
$primaryKey = 'project_id';
$columns = array(
array(
'db' => 'project_id',
'dt' => 'DT_RowId'
),
array(
'db' => 'creator_id',
'dt' => 'creator'
),
array(
'db' => 'creator_name',
'dt' => 'creator_name'
)
);
// SQL server connection information
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require(BASE_DIR.'/lib/dataTables/ssp.class.php');
$result = SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns);
echo json_encode($result);
To use that trick, you also need to edit ssp.class.php and replace all instances of FROM `$table` with FROM $table to remove backticks.
Alternatively, there is github.com/emran/ssp repository for library that extends ssp.class.php allowing GROUP BY, JOIN, aliases.
LINKS
See jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php for more information.

Make Select Options Static to Dynamic

Select's Option Values are static & i want it dynamic
Right now is static. as per below line
<?echo $this->Form->input('intaddressid',
array(
'options' =>
array('add1static,add1static' => 'add1static,add1static',
'addres2static,add2stattic'=>'addres2static,add2stattic'),
'label'=>false,
'empty' => '(Select hotel and time)',
'class' => 'form-control border_none'
)
);
?>
My static field output
https://www.dropbox.com/s/6133d4e9aorpo33/Selection_047.png?dl=0
And if i set for each then my dynamic
https://www.dropbox.com/s/v3hfhfaubkfr62m/Selection_048.png?dl=0
But i want it only in 1 field all value so help on it
I used for each times as time and my table value generated but now i want this value in above my static code. value => add1,add2 = add1,add2
$times['Time']['varaddress1']
$times['Time']['varaddress2']
my updated code is ->
$arr1=array();
$arr2=array();
$arr1 = $this->Time->find('list', array(
'fields' => array('varaddress1'),'order'=> array('Time.varaddress1')
));
$arr2 = $this->Time->find('list', array(
'fields' => array('varaddress2'),'order'=> array('Time.varaddress2')
));
$finalArr=array_merge ($arr1,$arr2);
$this->set('time',$finalArr);
and in ctp i used
'options' => $time
In ctp
<?php
echo $this->Form->input('intaddressid', array(
'options' => $courseCategoriesNames,
'empty' => '(choose one)',
'label' => false
));
?>
UPDATED solution
In controller:
NEW
$arr1=array();
$arr2=array();
$arr1= $this->Course->CourseCategory->find('list',
array('fields'=> array('Time.varaddress1'),
'order'=> array('Time.varaddress1') )
);
$arr2= $this->Course->CourseCategory->find('list',
array('fields'=> array('Time.varaddress1'),
'order'=> array('Time.varaddress2') )
);
$finalArr=array_merge ($arr1,$arr2);
$this->set('courseCategoriesNames',$finalArr);
FINAL ans
//INSIDE model Your Time->
public $virtualFields = array('name_price' => 'concat(Time.varaddress1, "-", Time.varaddress2)');
////INSIDE model
$products=$this->Product->find('list',array('fields'=> $this->Time->virtualFields['name_price'] )));
$this->set('products',compact($products));

Wordpress Multiple meta_key (WPDB or WP_Query)

Attempting to test whether a post has a 'main_slider', 'flickr-slider', or 'video-slider' value. 'main_slider' is a String, 'flickr-slider' and 'video-slider' are both Boolean.
This is what I have so far, which doesn't work a lick...
$slider = new WP_Query(
array(
'ignore_sticky_posts' => 1,
'post_type' => 'any',
'orderby' => 'date',
'nopaging' => true,
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'main_slider'
),
array(
'key' => 'flickr-slider'
),
array(
'key' => 'video-slider'
)
)
)
);
Thank you for any help in advance.
I decided to go with a workaround. I called each meta_key independently, merged while stripping out duplicate post, and then sorted by date. I'm sure its a heavier load on the server, but it got the job done.