I had a similar problem and could solve it with the help of your
ZeroNullIdType.php.
Try this (type="ZeroNullId") in @Id property of Company entity:
<?php
/**
* @Entity @Table(name="company")
**/
class Company
{
/**
* @Id @Column(type="ZeroNullId") @GeneratedValue
**/
protected $id;
/**
* @OneToMany(targetEntity="Person", mappedBy="company",
cascade={"persist", "remove"})
* @var person[]
**/
protected $people;
// ...
}
/**
* @Entity @Table(name="person")
**/
class Person
{
/**
* @Id @Column(type="integer") @GeneratedValue
**/
protected $id;
/**
* @ManyToOne(targetEntity="Company", inversedBy="people")
* @JoinColumn(name="company_id", referencedColumnName="id")
**/
protected $company;
// ...
}
//.....
//TESTING
$objCompany = new Company();
$objPerson = new Person();
$objPerson->setCompany($objCompany);
$objCompany->addPerson($objPerson);
$em->persist($objCompany);
$em->flush();
//Inserted 1 Company, 1 Person with related Company ID
$objPerson = new Person();
$em->persist($objPerson);
$em->flush();
//Inserted 1 Person with ZERO Company ID instead of NULL
This works for my problem, I hope it helped you.
Thanks,
Douglas Gomes de Souza
www.douglasdesouza.com.br
Em terça-feira, 12 de fevereiro de 2013 20:15:58 UTC-2, Troy P escreveu:
>
> No good. Type isn't an acceptable property on @JoinColumn.
>
> PHP Fatal error: Uncaught exception
> 'Doctrine\Common\Annotations\AnnotationException' with message '[Creation
> Error] The annotation @JoinColumn declared on property Person::$company
> does not have a property named "type". Available properties: name,
> referencedColumnName, unique, nullable, onDelete, columnDefinition,
> fieldName'
>
> Cheers,
> Troy
>
> On 12/02/2013, at 7:46 PM, Marco Pivetta wrote:
>
> Give the `@JoinColumn` trick a chance first, but I cannot ensure that it
> will work
>
> Marco Pivetta
>
> http://twitter.com/Ocramius
>
> http://ocramius.github.com/
>
>
> On 12 February 2013 10:44, Troy Parker <[email protected] <javascript:>>
> wrote:
>
>> As I explained changing the database schema is not currently an option
>> (multiple systems accessing multiple databases and gigabytes of data).
>>
>> If it's not supported then we'll have to work something else out. Thank
>> you for taking the time to respond.
>>
>> Troy
>>
>> On 12/02/2013, at 7:32 PM, Marco Pivetta wrote:
>>
>> You will need to fix your database schema then, since this is not
>> supported. A non-null reference is a reference (and 0 != null). You can try
>> using an @JoinColumn(type="zeronullid") first
>>
>> Marco Pivetta
>>
>> http://twitter.com/Ocramius
>>
>> http://ocramius.github.com/
>>
>>
>> On 12 February 2013 10:10, Troy Parker <[email protected] <javascript:>>
>> wrote:
>>
>>> Thanks Marco.
>>>
>>> I know the association isn't a column, but it inevitably gets stored as
>>> a column. It's the stored column I need to define as "zeronullid".
>>>
>>> The invalid mapping below was trying to explain what I need to do.
>>>
>>> Some further e.g. on the data involved incase I'm not being clear.
>>>
>>> Company
>>> id,name
>>> 1,"My Company"
>>>
>>> Person
>>> id,name,company_id
>>> 1,"Troy",0
>>> 2,"Marco",1
>>>
>>> Project
>>> id,name,company_id
>>> 1,"My Project",NULL
>>>
>>> So in some places a NULL company is store as NULL, others it's stored as
>>> a ZERO. So I can't set the custom type on the Company table.
>>>
>>> Troy
>>>
>>> On 12/02/2013, at 7:02 PM, Marco Pivetta wrote:
>>>
>>> Associations are no columns. If your association is a "*zeronullid*",
>>> then you have to mark the referenced identifier as `@Column(type="
>>> *zeronullid*")`. Your current mapping is invalid
>>>
>>> Marco Pivetta
>>>
>>> http://twitter.com/Ocramius
>>>
>>> http://ocramius.github.com/
>>>
>>>
>>> On 12 February 2013 05:16, Troy Parker <[email protected] <javascript:>>
>>> wrote:
>>>
>>>> Hey guys,
>>>>
>>>> We have some tables which use 0 instead of NULL for no linked entity.
>>>> I would like to be able to map these using a custom doctrine type *on
>>>> the associated table*.
>>>>
>>>> I can set the type on the main entity which works perfectly except then
>>>> I am forced to use 0 across everything that uses that entity.
>>>>
>>>> Unfortunately this is a live and massive system so changing how those
>>>> values are stored isn't an option at this point in time.
>>>>
>>>> Thanks for any input!
>>>>
>>>> eg.
>>>> <?php
>>>> use Doctrine\Common\Collections\ArrayCollection;
>>>>
>>>> /**
>>>> * @Entity @Table(name="company")
>>>> **/
>>>> class Company
>>>> {
>>>> /**
>>>> * @Id @Column(type="integer") @GeneratedValue
>>>> **/
>>>> protected $id;
>>>> /**
>>>> * @OneToMany(targetEntity="Person", mappedBy="company")
>>>> * @var person[]
>>>> **/
>>>> protected $people;
>>>>
>>>> // ...
>>>> }
>>>>
>>>> /**
>>>> * @Entity @Table(name="person")
>>>> **/
>>>> class Person
>>>> {
>>>> /**
>>>> * @Id @Column(type="integer") @GeneratedValue
>>>> **/
>>>> protected $id;
>>>>
>>>> /**
>>>> * @ManyToOne(targetEntity="Company", inversedBy="people")
>>>> * * @Column(type="zeronullid") *
>>>> * @JoinColumn(name="company_id", referencedColumnName="id")
>>>> **/
>>>> protected $company;
>>>>
>>>> // ...
>>>> }
>>>>
>>>> // ZeroNullIdType.php
>>>> namespace My\Types;
>>>>
>>>> use Doctrine\DBAL\Types\Type;
>>>> use Doctrine\DBAL\Types\BigIntType;
>>>> use Doctrine\DBAL\Platforms\AbstractPlatform;
>>>>
>>>> class ZeroNullIdType extends BigIntType
>>>> {
>>>> const NAME = 'zeronullid';
>>>>
>>>> public function convertToPHPValue($value, AbstractPlatform
>>>> $platform)
>>>> {
>>>> if($value === 0) {
>>>> return null;
>>>> }
>>>>
>>>> return $value;
>>>> }
>>>>
>>>> public function convertToDatabaseValue($value, AbstractPlatform
>>>> $platform)
>>>> {
>>>> if($value === null) {
>>>> return 0;
>>>> }
>>>>
>>>> return $value;
>>>> }
>>>>
>>>> public function getName()
>>>> {
>>>> return self::NAME;
>>>> }
>>>> }
>>>>
>>>> --
>>>> 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] <javascript:>.
>>>> To post to this group, send email to [email protected]
>>>> <javascript:>.
>>>> Visit this group at http://groups.google.com/group/doctrine-user?hl=en.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> 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] <javascript:>.
>>> To post to this group, send email to [email protected]
>>> <javascript:>.
>>> Visit this group at http://groups.google.com/group/doctrine-user?hl=en.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>>
>>>
>>> --
>>> 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] <javascript:>.
>>> To post to this group, send email to [email protected]
>>> <javascript:>.
>>> Visit this group at http://groups.google.com/group/doctrine-user?hl=en.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>
>>
>> --
>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/doctrine-user?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>>
>>
>> --
>> 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] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/doctrine-user?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
> --
> 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] <javascript:>.
> To post to this group, send email to [email protected]
> <javascript:>.
> Visit this group at http://groups.google.com/group/doctrine-user?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
>
>
--
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.