I am very new to Cakephp 3. I try to do saveAll function in Cakephp 3, but unsuccessful.
I have 2 tables : Merchants and Users
Relationship between this 2 tables as below:
Users hasMany Merchants,
Merchants belongsTo Users
I want the create a merchant's registration form that will insert the data to both tables.
Can someone help me how to insert data into 2 different tables from 1 form using Cakephp 3?
My code as below:
<?= $this->Form->create($merchant); ?>
<fieldset>
<legend><?= __('Add Merchant') ?></legend>
<?php
echo $this->Form->input('User.id');
echo $this->Form->input('User.username');
echo $this->Form->input('User.password');
echo $this->Form->input('merchant_type_id', ['options' => $merchantTypes]);
echo $this->Form->input('name');
echo $this->Form->input('identity_number');
echo $this->Form->input('phone_number');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Controller:
public function registration()
{
$merchant = $this->Merchants->newEntity();
if ($this->request->is('post')) {
$merchant = $this->Merchants->patchEntity($merchant, $this->request->data, [
'associated' => ['Users']
]);
if ($this->Merchants->saveAll($merchant)) {
$this->Flash->success('The merchant has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The merchant could not be saved. Please, try again.');
}
}
$users = $this->Merchants->Users->find('list', ['limit' => 200]);
$merchantTypes = $this->Merchants->MerchantTypes->find('list', ['limit' => 200]);
$this->set(compact('merchant', 'users', 'merchantTypes'));
}
Your helps is very appreciated. Thanks.
I think your problem is that your fields aren't named by lower-underscored version of the association name.
Instead of put:
echo $this->Form->input('User.username')
Which would result:
<input type="text" name="User[username]" >
You should try to put something as:
echo $this->Form->input('user.username');
Which would result:
<input type="text" name="user[username]" >
I hope it would be useful :)
PS: Sorry if there's any grammar mistakes but English is not my native language ;)
Please follow the documentation for saving data:
http://book.cakephp.org/3.0/en/orm/saving-data.html
If you are coming from 2.x, read the ORM migration guide, it should answer most of your questions:
http://book.cakephp.org/3.0/en/appendices/orm-migration.html
Related
I'm working on a Yii project with a database, that contains a table, where almost all it's data is saved in a field as JSON (it's crazy, but it is so as it is):
id INTEGER
user_id INTEGER
data LONGTEXT
This "JSON field" data has following structure and contains inter alia an image:
{
"id":"1",
"foo":"bar",
...
"data":{
"baz":"buz",
...
}
}
Displaying it is no problem, but now I want to make the data ediable. My form looks like this:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'my-form',
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'enableAjaxValidation'=>false,
));
?>
<div class="row">
<?php echo $form->labelEx($model, 'foo'); ?>
<?php
echo $form->textField($model, 'foo', array(...));
?>
<?php echo $form->error($model, 'foo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model, 'baz'); ?>
<?php
echo $form->textField($model, 'data[baz]', array(...));
?>
<?php echo $form->error($model, 'data[baz]'); ?>
</div>
It works. But there are multiple problems, that seem to be caused by the same thing -- that he form fields are not referenced to the model attributes/properties:
When I make fields foo and baz required (public function rules() { return array(array('foo, baz', 'required')); } -- the property $foo is defined) foo bahaves as wished, but baz causes an "foo cannot be blank" error. So I cannot set a data[*] as required.
If the form is not valid and gets reloaded, all the data[*] fields are empty.
The data[*] fields are not marked as required.
Is there a to solve this without to change the datase structure? There will not be a correct way for it, but maybe a workaround.
It's impossible to validate fields in such way. First of all if you are using field in model it must be defined or exist in table for active record. So if you want to validate such structure the only right way to do it:
class Model extends CActiveRecord {
// Define public varialble
public $data_baz;
public function rules(){
return array(
// Add it to rules
array( 'data_baz', 'required' )
);
}
public function attributeLabels(){
return array(
// Add it to list of labels
'data_baz' => 'Some field'
);
}
protected function beforeSave(){
if ( !parent::beforeSave() ) {
return false;
}
// Also you may create a list with names to automate append
$this->data['baz'] = $this->data_baz;
// And serialize data before save
$this->data = serialize( $this->data );
return true;
}
}
And your form should looks like
<div class="row">
<?php echo $form->labelEx($model, 'data_baz'); ?>
<?php echo $form->textField($model, 'data_baz'); ?>
<?php echo $form->error($model, 'data_baz'); ?>
</div>
I'm working on a Yii project with a database, containing a table, where almost all it's data is saved in a field as JSON (it's crazy, but it is so as it is):
id INTEGER
user_id INTEGER
data LONGTEXT
This "JSON field" data has following structure and contains inter alia an image:
{
"id":"1",
"foo":"bar",
...
"bat":{
"baz":"buz",
"name":"Joe Doe",
"my_picture":"iVBORw0KGgoAAAANSUhEUgAAAGQA...", <-- binary
...
}
}
Displaying it is no problem, but now I want to make the data ediable. My form looks like this:
<?php
$form=$this->beginWidget('CActiveForm', array(
'id' => 'insurance-form',
'htmlOptions' => array('enctype' => 'multipart/form-data'),
'enableAjaxValidation'=>false,
));
?>
<div class="row">
<?php echo $form->labelEx($model, 'provider_name'); ?>
<?php
echo $form->textField($model, 'data[provider][name]', array(
'size'=>60, 'maxlength'=>255, "autocomplete"=>"off"
));
?>
<?php echo $form->error($model, 'data[provider][name]'); ?>
</div>
It works.
I know, that for image upload I need fileField(...), but cannot find out, how to configure it in order to save the image directly to the database. How to do his?
view
<div class="row">
<?php echo $form->labelEx($model, 'provider_name'); ?>
<?php
echo $form->fileField($model, 'data[provider][name]', array());
?>
<?php echo $form->error($model, 'data[provider][name]'); ?>
</div>
controller
public function actionUpdate($id)
{
$model = $this->loadModel($id);
if(isset($_POST['External'])) {
$modelDataArray = $model->data;
// adding the image as string to the POSted data
if (isset($_FILES['MyModel']['name']['data']['provider']['picture'])) {
$_POST['MyModel']['data']['provider']['picture'] = base64_encode(
file_get_contents($_FILES['MyModel']['tmp_name']['data']['provider']['picture'])
);
}
$inputFieldData = $_POST['MyModel']['data'];
$updatedDataArray = array_replace_recursive($modelDataArray, $inputFieldData);
$model->attributes = $_POST['MyModel'];
$updatedDataJson = json_encode($updatedDataArray);
$model->setAttribute('data', $updatedDataJson);
if($model->save()) {
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('update', array(
'model' => $model,
));
}
CActiveRecord model
no special changes
I am working with configurable product, I want simple product id of configurable product on cart.phtml, I am using this code
<?php foreach($this->getItems() as $_item): ?>
$_product = $_item->getProduct();
echo $_product->getId();
<?php endforeach ?>
but it always gives main product id, but I need its simple product id , any one help please.
I found solution
<?php foreach($this->getItems() as $_item): ?>
$_COnfigproduct = $_item->getProduct();
$simpleProduct=Mage::getModel('catalog/product')->loadByAttribute('sku',$_item->getSku());
echo $simpleProduct->getId();
<?php endforeach ?>
it works for me.
you can get simple product id associated to configurable product on cart page..
by below code
<?php $_item = $block->getItem();
$product = $_item->getProduct();
if($product->getTypeId() == 'configurable') {
echo $_item->getOptionByCode('simple_product')->getProduct()->getId();
} ?>
I have a problem, i'm trying to render 2 forms (login and register) on one layout scrpt (header.phtml), every time i submit on one of the forms both actions for the controller are getting fired and i'm unsure how to fix it.
The forms are getting rendered fine within the layout, however when you click 'Login' or 'Register' on the forms the code fires in both the 'login' and 'register actions.
the header layout script snippet:-
<div class="left">
<h1>Already a member? <br>Then Login!</h1>
<?php
echo $this->action('panlogin', 'user');
?>
</div>
<div class="left right">
<h1>Not a member yet? <br>Get Registered!</h1>
<?php
echo $this->action('panregister', 'user');
?>
</div>
the action scripts (phtmls)
panregister.phtml
<div id="pan-register">
<?php
$this->registerForm->setAction($this->url);
echo $this->registerForm;
?>
</div>
panlogin.phtml
<div id="pan-login">
<?php
$this->loginForm->setAction($this->url);
?>
</div>
the user controller actions:-
class Ajfit_UserController extends Zend_Controller_Action
{
protected $_loginForm;
protected $_registerForm;
public function init()
{
$this->_loginForm = new Ajfit_Form_User_Login(array(
'action' => '/user/login',
'method' => 'post',
));
$this->_registerForm = new \Ajfit\Form\User\Registration(array(
'action' => '/user/register',
'method' => 'post'
));
}
//REGISTER ACTIONS
public function panregisterAction(){
$this->registerAction();
}
public function registerAction(){
$request = $this->_request;
if ($this->_request->isPost()){
$formData = $this->_request->getPost();
}
$this->view->registerForm = $this->_registerForm;
}
//LOGIN ACTIONS
public function panloginAction(){
$this->loginAction();
}
public function loginAction(){
$request = $this->_request;
if(!$auth->hasIdentity()){
if ($this->_request->isPost()){
$formData = $this->_request->getPost();
}
}
$this->view->loginForm = $this->_loginForm;
}
}
Please can someone with a little more knowlegde with the action('act','cont'); ?> code with in a layout script help me out with this problem.
Thanks
Andrew
While David is correct where best practices are concerned, I have on occasion just added another if() statement. Kinda like this:
if ($this->getRequest()->isPost()) {
if ($this->getRequest()->getPost('submit') == 'OK') {
just make sure your submit label is unique.
Eventually I'll get around to refactoring all those actions I built early in the learning process, for now though, they work.
Now to be nosy :)
I noticed: $formData = $this->_request->getPost(); while this works, if you put any filters on your forms retrieving the data in this manner bypasses your filters. To retrieve filtered values use $formData = $this->getValues();
from the ZF manual
The Request Object
GET and POST Data
Be cautious when accessing data from the request object as it is not filtered in any way. The router and
dispatcher validate and filter data for use with their tasks, but
leave the data untouched in the request object.
From Zend_Form Quickstart
Assuming your validations have passed, you can now fetch the filtered
values:
$values = $form->getValues();
Don't render the actions in your layout. Just render the forms:
<div class="left">
<h1>Already a member? <br>Then Login!</h1>
<?php
echo new \Ajfit\Form\User\Login(array(
'action' => '/user/login',
'method' => 'post'
));
?>
</div>
<div class="left right">
<h1>Not a member yet? <br>Get Registered!</h1>
<?php
echo new \Ajfit\Form\User\Registration(array(
'action' => '/user/register',
'method' => 'post'
));
?>
</div>
Then, whichever form gets used will post to its own action.
I have two controllers partners and deals.
When I add deal there is field partner id and in cakephp this generates a dropdown with all the partners. Is there away that if the user wants to add a new partner they could click a checkbox and add form would appear. Tried adding the partner input boxes and yes this creates new partner but it in the deal table it puts the partner id of the selected partner from the drop down not the new partner.
Deal Veiw
<div class="deals form">
<h2>Add New Deal</h2>
<p>Use the form below to fill in the new deals details.</p>
<?php echo $form->create('Deal');?>
<?php
echo $cksource->create();
echo $form->input('title');
echo $form->input('price');
echo $form->input('market_price');
echo $form->input('discount');
echo $form->input('buy_link');
echo $form->input('image');
$config['toolbar'] = array(
array( 'Source', '-', 'Bold', 'Italic', 'Underline', 'Strike', '-','BulletedList' ),
array( 'Image', 'Link', 'Unlink', 'Anchor' )
);
?><label>Deal Highlights</label><?php
echo $cksource->ckeditor('highlights', array('config'=>$config));
?><label>Deal Fine Print</label><?php
echo $cksource->ckeditor('fine_print', array('config'=>$config));
echo $form->input('description');
?><hr />
<h3>Partners Details<?php
echo $form->input('partner_id');
echo $form->input('Partner.name');
echo $form->input('Partner.address1');
echo $form->input('Partner.city');
echo $form->input('Partner.county');
echo $form->input('Partner.postcode');
echo $form->input('city_id');
?><hr />
<h3>Schedule Deal<?php
echo $form->input('start');
echo $form->input('end');
echo $cksource->end();
?>
<?php echo $form->end(__('Submit', true));?>
</div>
Deal Controller
function admin_add() {
if (!empty($this->data['Partner']['name'])) {
$this->data['Deal']['partner_id'] = "";
if ($this->Deal->Partner->saveAll($this->data)) {
$this->Session->setFlash(__('The deal has been saved', true));
$this->redirect(array('controller'=>'deals', 'action' => 'add'));
} else {
$this->Session->setFlash(__('The deal could not be saved. Please, try again.', true));
}
} else {
if ($this->Deal->saveAll($this->data)) {
$this->Session->setFlash(__('The deal has been saved', true));
$this->redirect(array('controller'=>'deals', 'action' => 'add'));
}
}
$partners = $this->Deal->Partner->find('list');
$cities = $this->Deal->City->find('list');
$this->set(compact('partners', 'cities'));
}
Any Ideas guys? If you know a better way to do it would be happy to hear it,
Thanks
Dave
in controller before saving, check if the partner form is not empty, then empty out the $this->data['Deal']['partner_id'];