I have the following code in a controller action:
public function someAction()
{
// ...
$promo_repo = $em->getRepository('AcmeContactlistBundle:Promotion');
$default_promo_code = $this->container->getParameter('promo_default_code');
$promo = $promo_repo->findOneByCode($default_promo_code);
if (empty($promo)) {
$promo = new Promotion();
$promo->setCode($default_promo_code);
$start_date = $this->container->getParameter('promo_default_start_date');
$expiry_date = $this->container->getParameter('promo_default_end_date');
$promo->setStartsAt(new \DateTime($start_date));
$promo->setExpiresAt(new \DateTime($expiry_date));
$em->persist($promo);
$em->flush();
}
$contact = new Contact();
$contact->setPromotion($promo); // <- Error here. See error msg below
// Some more code follows ...
}
Error Message
PHP Catchable fatal error: Argument 1 passed to
Acme\ContactlistBundle\Entity\Contact::setPromotion() must be an
instance of Acme\ContactlistBundle\Entity\Promotion, null given,
...
I have checked the SQL generated by the above statements and also checked to make sure that a record is persisted to the promotion table (if not present). The promotion object is created and persisted to the database correctly, so I don't understand that by the times it comes to assigning it to the contact variable, it has a value of null.
What is going wrong?
Related
I'm working with 'Parse.com' and I would like to store a value into a column in a Class that points to another Class.
For example:
$user = new ParseUser();
$user->set("username", "Batman");
$user->set("password", "darkknight");
$user->set("vehicle_ID", "UgTuNHEQEZ"); //pointer
Here, "vehicle_ID" is the name of the column. It's a pointer to the column id in the Class "Vehicle".
The error I get is that Parse expected the name of the Class I'm referring to. So, if I change the last line and do:
$user->set("Vehicle", "UgTuNHEQEZ");
The new user is created BUT the "vehicle_ID" is empty. Why is vehicle_ID not populated?
Double handicapped by not knowing php and discovering to my dismay that the parse.com API reference is a link to the github source, I did notice in the source a constructor function as follows:
public function __construct($className = null, $objectId = null,
$isPointer = false
) {
...
This suggests:
$vehicle = new ParseObject("OtherClass", "UgTuNHEQEZ", true);
$user->set("vehicle_ID", $vehicle);
I am building some webapp. I don't want the user to rewrite value in database if it already is there. I am trying to check this with preSave() behavior. Whenever i try to get the value from database i get the value the user just posted in with $_POST[]
public function preSave(\PropelPDO $con = null) {
$obj = new UserQuery();
//let's check if user has set the value
$type = $obj->findOneById($this->getId())->getType();
var_dump($type);// <-- here the value from database is always as user
//have just been selected in form. The value in database
//actually is NULL
//if type in database have been saved then set the current type from database
if(!is_null($type))
$this->setType($type);
return true;
}
So my question is: Why the value from database is never NULL, but some TRUE or FALSE as user have picked in form? It should be NULL cause in that moment save() haven't been executed yet.
I suppose, you're getting the same object because of the Propel instance pooling. It just caches the previously fetched objects. So, you can try to disable instance pooling for the preSave method:
public function preSave(\PropelPDO $con = null) {
\Propel::disableInstancePooling();
// your code...
\Propel::enableInstancePooling();
}
I have come across this Error I've not seen before:
Message: SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters
Referring to the following code (have simplified the function for ease of reading):
if ($frm->isValid($this->_getAllParams()) || !count($frm->getMessages())) //error points to this line of an array
{
//set session with id of user
$session = new Zend_Session_Namespace('rg');
$session->userid = $this->getRequest()->getPost('id');
//update the user
$mdl->createClient($this->_getAllParams());
//add to log - do in model
$this->_redirect('/.../...');
}
6 C:\xampp\htdocs\portal-gep-2\library\Null\Validate\Db\NoRecordExists.php(7): Zend_Validate_Db_NoRecordExists->isValid('7505020152089')
class Null_Validate_Db_NoRecordExists extends Zend_Validate_Db_NoRecordExists
{
public function isValid($value)
{
$response = parent::isValid($value);//this line
if(!$response){
$this->_messages =
array(self::ERROR_RECORD_FOUND=> "Please correct this error before continuing <a href='/data-control/idnum/id/$value'>Correct Issue</a>");
}
return $response;
}
}
The form in question has a constructor of the form:
public function __construct($formState = 'createlocal', $currentTask = null)
based on whether the form is updating an existing record or a new record it will make use of a custom validator: Null_Validate_Db_NoRecordExists.
In the specific case you talk of the form is using a Db_NoRecordExists on an existing record and hence we whould like to exclude a specific record (the current one) using one of the constructor parameters in this case: $currentTask.
$currenctTask may be an array and the validator may make use of an array variable while ensuring the Record does not exist (in the where clause). However the variable is not parsed when initially constucting the form.
Hence check that $currentTask contains what it needs to contain. The error is of your own making.
I need to compare if a certain property of my object has changed when someone is saving it. I wrote a plugin to be able to add some functionality before and after updating an object in the backend happens.
So, I don't know if this is not working as expected or if I'm getting this wrong.
I thought I would get the state before it is saved to the database in:
function preUpdateObject(Object_MyObject $object) {}
And the new state of the object in
function postUpdateObject(Object_MyObject $object) {}
But this doesn't work:
public function preUpdateObject(Object_MyObject $object) {
$this->tempOldDate = $object->getUpdate();
}
public function postUpdateObject(Object_MyObject $object){
if($this->tempOldDate->compareDate($object->getUpdate()) == -1) {
// do something because a newer date has been entered
}
}
Any clue how I can get the old object state BEFORE it is updated??
Seems like this function is not working as expected. I filed a bug report:
http://www.pimcore.org/issues/browse/PIMCORE-1232
I created a workaround that can be used.
The database is NOT updated. So it's correct that preUpdateObject is the function to use to get the object state, but:
Check that database is not updated.... I want a date from my object that sits in table 'object_1'
// oldDate has the old value
$dbAdapter = Pimcore_Resource_Mysql::get("database");
$dbentry = $dbAdapter->fetchRow(
$dbAdapter->select()
->from('object_1')
->where('o_id = ?', $object->getId()));
$oldDate = new Pimcore_Date($dbentry['update']);
Using the generated Object class doesn't work, but if you clear the cache it does
// get's the new date from the editor
$oldDate = Object_MyObject::getById($object->getId())->getUpdate();
// this also works but don't know if it is safe to delete
// the object from the registry
Zend_Registry::set('object_' . $object->getId(), false);
$oldDate = Object_MyObject::getById($object->getId())->getUpdate();
I'm trying to dynamically create object properties for JSON representation of an object. The class User will feature some default properties (setted in __construct). I'm using custom object instead of arrays because i prefer object oriented style (and i need also custom setter/getters methods).
However the first try gives me:
Strict standards: Creating default object from empty value.
even if the code actually works (and json_encode shows the right output):
<?php
class User
{
protected $data = array();
public function __set($property, $value)
{
$this->data[$property] = $value;
}
}
$u = new User();
$u->name = "James Smith"; // Works
$u->status->active = false; // Fail
$u->status->modified = time();
var_dump(json_encode($u));
?>
I think it's because the call $u->status->active, when property $u->status does not exist yet. Do you know how to fix this?
OK I sorted that out for you :) It was interesting.
First, you have not initialized the status property. So in theory, this should have been sufficient:
$u->status = new StdClass;
However, it is more complicated than this. Even if you do it, it won't work. That is because you are setting your fields in the data array, but you are never GETTING THEM OUT from there!
So when you access a field ($u->status) you are NOT taking the field you have just set: you are accessing an unset object property. If you try to print $u->name after setting it, you will not get anything, because you have not created a getter function which would read your data array.
You should either create a getter, or delete the setter (it will work anyway, but may not be what you need).
If you comment out the setter, everything works without warnings. See this simplified version:
<?php
error_reporting(E_STRICT);
class User
{
}
$u = new User();
$u->name = "James Smith";
$u->status = new StdClass; // Comment this line and you will get the strict warning
$u->status->active = false;
var_dump($u);