I have a user submit a field to the database, it validates, and makes the entry. The primary key of this new row is auto-incremented.
The user then gets to another form where that newly created field is required.
Can anyone shed any light on this problem?
Thanks in advance!
Check out Model::getInsertID();
$this->getLastInsertID();
cakephp model docs
// Save the first form
$this->Ingredient->save($newData);
// Get the id of the record just saved
$newIngredientId = $this->Ingredient->id;
// Redirect to a new form
$this->redirect(array(
'controller' => 'dish',
'action' => 'add',
'?' => array('lastId' => $newIngredientId)
));
http://book.cakephp.org/view/1031/Saving-Your-Data,
http://book.cakephp.org/view/1442/link
If it's in the database, when you get to your next form just read it out again in your controller and set it to a view variable.
$this->set('lastid', $this->Model->read(null,$id));
Or if you need to search the database for the field use find
$this->set('lastid', $this->Model->find('first', array('conditions'=>array('username'=>'MyUserName')) , array('id')));
You could go around it by using mysql_insert_id()
Related
I made a custom field in prestashop.
I would like to know if there is some way to make this field only required on the frontend and not for administrators.
this is my code in Customer class
array('type' => self::TYPE_STRING, 'required' => true, 'size' => 64),
Thanks in advance
Can't think of a quick way to make this possible exactly as you want it, so here are two suggestions:
1 - Remove the 'required' => true from the definition and create some kind of custom validation like if (Tools::getValue('type ') == "") { return false; } in an override of the front office AuthController (Warning: code is completely untested and should definitely be improved, also according to your particular PS version).
2 - Use frontend validation by giving the frontend form field the required attribute (you should do this even if oyu use 1)
here is the code that runs and that results in two sentmessage objects getting added to the table:
$sentmessage = null;
//add entry into sent message table Tx_BpsMessagecentre_Domain_Model_Sentmessage
$sentmessage = $this->objectManager->create('Tx_BpsMessagecentre_Domain_Model_Sentmessage');
//now just fill in the object
$sentmessage->setBpsmessageid($bPSMessage->getUid());
$sentmessage->setBody($bPSMessage->getBody());
$sentmessage->setSubject($bPSMessage->getSubject());
$sentmessage->setCouponlist($bPSMessage->getCouponlist());
$sentmessage->setHallname($bPSMessage->getHall()->getHall());
$sentmessage->setHalladdress($bPSMessage->getHall()->getAddress());
$sentmessage->setHallurl($bPSMessage->getHall()->getUrl());
$sentmessage->setBanner($bPSMessage->getBanner());
$this->sentmessageRepository->add($sentmessage);
$this->objectManager->get('Tx_Extbase_Persistence_Manager')->persistAll();
die; //if I take out this die and the persist call above I still get two records added
I am using typo3 v 4.5.32 with extbase 1.3.
The sentmessage object is one I had to create manually - ie without extension builder, so there might be some misconfiguration in the TCA somewhere but I have no idea what would cause this.
Thanks
PS: a piece of my ext_localconf.php showing some of my plugins
Tx_Extbase_Utility_Extension::configurePlugin(
$_EXTKEY,
'Bpsmonthly',
array(
'BPSMessage' => 'cronMonthlys'
),
// non-cacheable actions
array(
'BPSMessage' => 'create, update, delete',
)
);
Tx_Extbase_Utility_Extension::configurePlugin(
$_EXTKEY,
'Bpsbirthdays',
array(
'BPSMessage' => 'cronBirthdays'
),
// non-cacheable actions
array(
'BPSMessage' => 'create, update, delete',
)
);
Tx_Extbase_Utility_Extension::configurePlugin(
$_EXTKEY,
'BpsAnnuals',
array(
'BPSMessage' => 'cronAnnuals'
),
// non-cacheable actions
array(
'BPSMessage' => 'create, update, delete',
)
);
Ok, found the bug, it was actually caused by my debug code that spit some url out to the page, the url got resolved by the browser to hit the action again (probably a url in an img tag) and boom. so the bug only exists while debugging. I love programming.
Is there a way to delete my dumbest questions from stackoverflow?
most probably you have the two plugins from the same extension in your website - therefore everything is processed twice.
Maybe persisting your changes takes a loooooong time (e.g., >30 sec). In such cases, I experienced that under some conditions the browser posts again (!) the query, resulting in what you describe.
As I experienced the same behaviour, but under other circumstances, I'm adding my case as an answer here, maybe it helps someone.
If you're working with multiple records in the same form (model having child records) and they all are not persisted, make sure to not set the parent record on the child records.
This will lead to the multiple-rows behaviour:
$foo = ObjectManager->get('Foo\\Bar\\Domain\\Model\\Foo');
$bar = ObjectManager->get('Foo\\Bar\\Domain\\Model\\Bar');
$bar->setFoo($foo); // <-- Should not be done
$foo->add($bar);
I have a model InboxMessageHelper with relations like
'message', 'sender' and 'receiver' and I am using the following criteria to query data:
$model = new CActiveDataProvider('IndividualMessageHelper', array(
'criteria'=>array(
'condition'=>'receiver_id = '.Yii::app()->user->id,
'order'=>'message.created_at DESC',
'with'=>array('message', 'sender', 'receiver'),
'together'=>true,
),
));
I want to get all data (i.e. including the relations data) inside the controller and form a JSON, but the problem is that i cannot access related fields data. I can see that the data is available when I use
CVarDumper::dump()
when I try to encode $model->data then only data from current table gets encoded. How should I go about it?
I don't think CActiveDataProvider can be used in this way. You need to be working with the model. So you'll need something like this in your controller.
$models = IndividualMessageHelper::model()->findAll('receiver_id = '.Yii::app()->user->id);
foreach($models as $model){
$json[] = $model->getAttributes; //This won't get any model properties you've declared
yourself, only database columns
}
//Now get the related records and add them to the array.
array_push($json, $model->getRelated('message')->getAttributes());
array_push($json, $model->getRelated('sender')->getAttributes());
array_push($json, $model->getRelated('receiver')->getAttributes());
echo json_encode($json);
With GII I have created a list of records. I use the admin view so they are in a table view. On top of the table it is the search with a status for the records. When the status dropdown is changed I submit the form and the table gets searched. I want the default view of the admin to show only the active records so I want to create a link in the menu to this:
medium/admin/?Medium[status]=active
The actual link of course is
medium/admin/?Medium%5Bstatus%5D=active
I have tried to do it with:
CHtml::link('Mediums', array("medium/admin", array('Medium[status]' => 'active')))
CHtml::link('Mediums', array("medium/admin", array('Medium%5Bstatus%5D' => 'active')))
CHtml::link('Mediums', array("medium/admin", array('Medium' => array('status' => 'active'))))
But all of the links are incorrect so the default view of the table is with all the records shown.
What is the correct way to create such a link?
Thank you.
http://www.yiiframework.com/doc/api/1.1/CHtml#link-detail and http://www.yiiframework.com/wiki/48/ will be usefull for you.
CHtml::link(CHtml::encode('Mediums'),array("medium/admin", "status"=>"active"));
Then ensure that in your controller you have something like this:
public function actionAdmin($status)
Now you ca use 'status' in your action.
I'm using giix to extend model (and crud) behavior. In this I would like to handle columns of type timestamp (that already exist in my model) specifically, rather like autoincrement fields are handled. (Ignored and not shown, that is.) However, there is no property $column->isTimestamp. I would like to add this somewhere, but I'm rather at loss what the best place for this would be. Do I put it in giix somewhere, or do I have to extend the column-baseclass?
Edit: I want to ignore them from every view, for every table. Since this is a lot of work, and it's something I always want, I'd like to automate it. Adapting the generators seems to make most sense, but I'm not sure what the best way to do it would be.
Here is the process:
Extend your database schema, if you are on MySQL it is CMysqlSchema.
Extend CMysqlColumnSchema and add a "isTimestamp" attribute.
In your CMysqlSchema sub-class extend createColumn and test for a timestamp, you'll see that Yii makes simple string comparisons here to set it's own flags. Set "isTimestamp" in your CMysqlColumnSchema here.
Tell Yii to use your schema driver like this in your components section in the config:
'db' => array(
'connectionString' => 'mysql:host=localhost;dbname=database',
'username' => '',
'password' => '',
'driverMap' => array('mysql' => 'CustomMysqlSchema'),
),
You will need to query the column schema, I've not used giix but find where it is generating the views, it should be looping through either the model attributes or the underlying table schema.
If it is looping through the schema:
//you can also ask Yii for the table schema with Yii::app()->db->schema->getTable[$tableName];
if ('timestamp' === $tableSchema->columns[$columnName]->dbType)
continue; //skip this loop iteration
If it loops over the attributes:
$dbType = Yii::app()->db->schema->getTable[$model->tableName]->columns[$modelAttribute]->dbType;
if ('timestamp' === $dbType)
continue; //skip this loop iteration