I have a problem with the inheritance of Doctrine within my fixtures.yml file of Symfony.
Imagine the following:
NewsAuthor:
inheritance:
extends: sfGuardUser
type: simple
Now I want to declare the default guard user User_admin of the fixtures file of the sfDoctrineGuardPlugin to be the author of the first dummy article of my News model. But how to do this?
I tried the following:
NewsAuthor:
AuthorAdmin:
sfGuardUser: User_admin
But then I get this error of the doctrine:data-load task:
Unknown record property / related component "sfguarduser" on "NewsAuthor"
I use Symfony 1.4.8 with Doctrine 1.2.3.
Thank you in anticipation,
Lemmi
You're misunderstanding how Doctrine's inheritance works. Under simple inheritance, NewsAuthor wouldn't have a reference to sfGuardUser, it would have all of the columns and relations that sfGuardUser has plus the ones you define. Inheritance is a bad approach here.
Instead, you want a NewsAuthor to have a reference to sfGuardUser. Your schema in this case would look like:
NewsAuthor:
columns:
user_id:
type: integer(4)
notnull: true
unique: true #if one-to-one correspondence
relations:
User:
class: sfGuardUser
local: user_id
foreignType: one
Your fixtures would then look as they did before:
NewsAuthor:
AuthorAdmin:
sfGuardUser: User_admin
You could then access the sfGuardUser object on a NewsAuthor object via $newsAuthor->User, similarly, you can access NewsAuthor on an sfGuardUser instance via $user->NewsAuthor.
Related
I add two tables to db with this schema:
Receiptjob:
columns:
charity_id: integer
sf_guard_user_id: integer
created_at: timestamp
finished_at: timestamp
job_type: string(32)
job_status: integer
relations:
User:
class: sfGuardUser
Charity:
class: Charity
Spende:
class: Spende
refClass: ReceiptjobMap
type: many
local: receiptjob_id
foreign: spenden_id
ReceiptjobMap:
columns:
receiptjob_id: integer
spenden_id: integer
spenden_recurring: string(32)
relations:
Receiptjob:
class: Receiptjob
local: receiptjob_id
foreign: id
onDelete: CASCADE
Spende:
class: Spende
local: spenden_id
Everything works fine, untill i start my docker container this morning. I get allways this error message:
Uncaught Error: Call to a member function setTableName() on null in /var/www/vendor/lexpress/doctrine1/lib/Doctrine/Record/Abstract.php:140
I'll try to fix error but nothings helps, at least i build a new branch, added my schema.yml again an migrate it, but the error still alive....
what i'm doing wrong?
In my case, i was the problem, because i do not call parent::_construct in my class because in the BaseClass was no __construct. But BaseClass extented sfDoctrineRecord, and there is a __construct. So this was the reaseon while setTableName runs on null.
I'm working on a symfony 1.4 project which uses Doctrine 1.2. I want to add some custom property for fields inside my schema.yml. The purpose of this is to reuse schema.yml in some other place other than Doctrine, to maintain some meta data about the entity. I tried to add a customproperty as I shown in bellow sample.
TestEntity:
tableName: test_table
columns:
id:
type: integer(4)
primary: true
autoincrement: true
name:
type: string(200)
customproperty: true
But when I tried to do doctrine build model, now it fails giving the error as follows.
"Invalid schema element named "customproperty" at path "TestEntity->columns->name""
I checked the schema.php file in following location to debug the error.
symfony/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Import/Schema.php
This error comes because the custom property we are specifying is not there in "$_validation" array. Once I added the "customproperty" into that array it stopped giving the error.
But what I did was a hack to doctrine library and is there any other better way to achieve this without touching the library files.
I love doctrine but I have some problems with mapping/annotations. At start I used mapping files. Then I converted in into annotations. Now I want o create custom repository class so I did as I read here: http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes.
Unfortunately now I have an error:
No mapping file found named '\src\Vendor\ProductBundle\Resources\config\doctrine/SynchronizationSettingRepository.orm.yml' for class 'Vendor\ProductBundle\Entity\SynchronizationSettingRepository'.
Of course I don't have this file becouse I don't use mapping anymore.
I've added:
* #ORM\Entity(repositoryClass="Vendor\ProductBundle\Entity\SynchronizationSettingRepository")
to parent and regenerated entities. I have regenerate Entites by command php app/console doctrine:generate:entities VendorProductBundle and still nothing. Regural and doctrine meadata cache is clear.
Here is a YML which from I want to generate custom Repository one more time:
Vendor\ProductBundle\Entity\SynchronizationSetting:
type: entity
table: synchronization_setting
repositoryClass: Vendor\SynchronizationSetting\Entity\SynchronizationSettingRepository
indexes:
id_product:
columns:
- id_product
id:
id:
type: integer
nullable: false
unsigned: true
comment: ''
id: true
generator:
strategy: IDENTITY
fields:
open:
type: string
nullable: true
length: 1
fixed: true
comment: ''
default: '0'
internet:
type: string
nullable: true
length: 1
fixed: true
comment: ''
default: '0'
manyToOne:
idProduct:
targetEntity: Product
cascade: { }
mappedBy: null
inversedBy: null
joinColumns:
id_product:
referencedColumnName: id
orphanRemoval: false
lifecycleCallbacks: { }
And here is a repository class:
<?php
// src/Acme/StoreBundle/Entity/ProductRepository.php
namespace Vendor\ProductBundle\Entity;
use Doctrine\ORM\EntityRepository;
class SynchronizationSettingRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQuery(
'SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC'
)
->getResult();
}
}
I think that adding #ORM\Entity did very little good as complete .php class file gets overwritten as soon as you run the doctrine:generate:entities. You need to add repositotyClass to your YML file instead.
If you switched to annotations for good, those .yml files (actually whole doctrine directory within config) are useless, apart as being intermediate files for generating annotaion-based entities.
Another thing: It seams that Doctrine thinks you have a entity with name ending with "Repository".
Can you show us the content of YML file? If you don't have it (as you said), generating entities will not be possible. You can always generate annotaion-based entities directly (no need for YML intermediates)
Given this setup for my Doctrine 2 Entities:
App\Bundle\LorumBundle\Entity\Node:
type: entity
table: node
fields:
id:
id: true
type: integer
unsigned: false
nullable: false
generator:
strategy: IDENTITY
created:
type: datetime
inheritanceType: SINGLE_TABLE
discriminatorColumn:
name: type
type: string
length: 255
discriminatorMap:
a: a
b: b
c: c
App\Bundle\LorumBundle\Entity\A:
type: entity
fields:
status:
type: boolean
App\Bundle\LorumBundle\Entity\B:
type: entity
fields:
status:
type: boolean
App\Bundle\LorumBundle\Entity\C:
type: entity
fields:
title:
type: string
Now what I want to get is basically a mixed list of Entities of the type A & B (not C) with the status == true.
I could write a Query like this - using the instance ofoperator to limit the result to the subclasses i want of course but i will get an Error because the property I want to match against (status) is not mapped in the Superclass even tough all the Entities i want to match against have it:
$queryBuilder->select('Node');
$queryBuilder->from('App\Bundle\LorumBundle\Entity\Node','Node');
$queryBuilder->add('where',$queryBuilder->expr()->orx(
'Offer INSTANCE OF AppLorumBundle:A',
'Offer INSTANCE OF AppLorumBundle:B'
));
$queryBuilder->where($queryBuilder->expr()->eq('Node.status', '?1'));
$queryBuilder->setParameter(1, true);
$queryBuilder->orderBy('Node.created', 'asc');
$queryBuilder->setFirstResult( 0 );
$queryBuilder->setMaxResults( 200 );
Is there any way to do this, short of writing your own persister and hack it into Doctrine2?
Unfortunately its not an Option for me to just add the Information to the Superclass (in my real scenario this situation mostly applies to relations which i don't want to be loaded eagerly with every subclass)
You can use UNION and work with queries for entities A and B
build query for entity A
build query for entity B
build query for limit and order using UNION for queries A and B
no need to do any hacks for Doctrine library
A:
columns:
id: ...
name: type ....
b_id: type ...
actAs:
Sluggable:
unique: true
fields: [name, b_id]
canUpdate: true
relations:
B: { foreignAlias: As }
B:
columns:
id : ...
name: ...
Above the sort of schema =). It at least should give you the idea.
Desigred slug format "A.name-B.name"
I have written a csv importer where field A.slug is generated correctly( I haven't done anything to adjust it, all went by default ).
But I am facing issue when saving the record in the backend(symfony admin generator). It is generating the slug as A.name-B.id.
Q: What is the best way/direction to handle this case, so I have normal slugs without IDs
I believe it works to define the relation as part of the sluggable, rather than the foreign key column, i.e.:
Sluggable:
fields: [name, B]
Provided you have a __toString() method on B. If that doesn't work, or if you want more complex logic, you can use the builder option:
Sluggable:
builder: [ATable, buildSlug]
You would then define in ATable:
public static function buildSlug($proposal, $instanceofA)
{
return $instanceofA->name . '-' . $instanceofA->B->name; //do any other case altering, symbol removing here as well
}