Easiest solution is probably to just set the ID in the constructor,
like in 
http://docs.doctrine-project.org/en/2.0.x/tutorials/composite-primary-keys.html#primitive-types-only

$banned = new Entities\BannedEmail('[email protected]');
$em->persist($banned);
$em->flush();

And of course setting $this->rule = $rule in the constructor.

As I'm always talking about names: you have an email address and call it a 
rule. I would call an email adress an emailAddress, or maybe a 
bannedEmailAdress or some other informative name. An address is not a rule. 
That will make your model more clear. 


On Saturday, 19 April 2014 09:36:20 UTC+2, Herman Peeren wrote:
>
> OK, finally got something: the problem is with manually setting the ID of 
> an entity.
>
> When setting the ID of an entity and then flushing it, the entitymanager 
> first tries to find an entity with that ID. It doesn't find it, so it 
> replaces the ID with NULL. Then the record is inserted into the database 
> and boooom: error.
>
> See a.o. https://github.com/doctrine/DoctrineModule/issues/261
> And: https://groups.google.com/forum/#!topic/doctrine-dev/__QiHYhes38
> In the latter a solution/workaround is suggested, based on 
>
> http://stackoverflow.com/questions/5301285/explicitly-set-id-with-doctrine-when-using-auto-strategy
> (that is with auto-increment, but the core problem is the same: explicitly 
> setting the ID).
>
> Unfortunately have no time left to look in it further now, but hopefully 
> you are on the right track to figure it out yourself from here.
>
> On Friday, 18 April 2014 21:35:57 UTC+2, Parsifal wrote:
>>
>>  
>> I remove uniqueConstraints and changed it to:
>> $metadata->setPrimaryTable(array(
>>    'name' => 'banned_emails',
>> ));
>> $metadata->mapField(array(
>>    'fieldName' => 'rule',
>>    'columnName' => 'rule',
>>    'type' => 'string',
>>    'nullable' => false,
>>    'unsigned' => false,
>>    'comment' => '',
>>    'id' => true,
>> ));
>>  
>> And I am still getting this:
>>  
>> "START TRANSACTION" INSERT INTO banned_emails (rule) VALUES (null) 
>> "ROLLBACK" 
>> Fatal error: Uncaught exception 'PDOException' with message 
>> 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'rule' cannot 
>> be null' in /vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:165 Stack 
>> trace: #0 /vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php(165): 
>> PDOStatement->execute(NULL) #1 
>> /vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php(279):
>>  
>> Doctrine\DBAL\Statement->execute() #2 
>> /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(952): 
>> Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts() #3 
>> /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(335): 
>> Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata))
>>  
>> #4 /vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(389): 
>> Doctrine\ORM\UnitOfWork->co in 
>> /vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php on line 91
>>  
>> The code I am using is:
>> $banned = new Entities\BannedEmail;
>> $banned->setRule('[email protected]');
>> $em->persist($banned);
>> $em->flush();
>>  
>> and the method within class is:
>>     public function setRule($email)
>>     {
>>             $this->rule = $email;
>>     }
>>  
>> any idea?
>>  
>>  
>>  
>>
>>
>> On Fri, Apr 18, 2014 at 11:38 PM, Herman Peeren <[email protected]>wrote:
>>
>>> On Friday, 18 April 2014 18:09:11 UTC+2, Parsifal wrote:
>>>>
>>>> I guess doctrine actually requires an integer primary key? 
>>>>
>>>
>>> No, that is certainly not the case, fortunately. 
>>>
>>>  On Friday, 18 April 2014 18:09:11 UTC+2, Parsifal wrote:
>>>
>>>> or there should be some mistakes in $metadata->mapField?
>>>> Anyway, I did not write it myself it was generated by convert-mapping, 
>>>> I just created a table with rule as primary key and generated 
>>>> convert-mapping. what is wrong there? should I change something in 
>>>> $metadata->mapField or in db?
>>>>
>>>
>>> Yes, but I don't know exactly what happens. A wild guess: making the 
>>> rule-field the id in the mapField() makes that column already needing  
>>> unique values.  So there is no need for that uniqueConstraints-attribute in 
>>> the Table-setting. Maybe by also defining that it wants to generate a 
>>> rule-column besides the primary key. It's just a bit vague idea. I would 
>>> first try to leave that uniqueConstraints-part out. Do you then still get 
>>> the same error-message? 
>>>  
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "doctrine-user" group.
>>> To unsubscribe from this group and stop receiving emails from it, send 
>>> an email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/doctrine-user.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to