I need to remove a dash from the Sluggable field. But it is needed only for Company table, as there are another tables that use this option, I do not want to change them. So if I insert "ABC Studio", I need to get "ABCStudio".
Now I get "abs-studio"
Company:
actAs:
Timestampable:
Sluggable:
fields: [name]
canUpdate: true
Sluggable has option named builder, which is by default set to ('Doctrine_Inflector', 'urlize') - that is, Doctrine_Inflector::urlize($text) is called. You can set it to your own:
Company:
actAs:
Timestampable:
Sluggable:
fields: [name]
canUpdate: true
builder: [CompanyTable, urlize]
Related
I have 2 tables. One let's say organizations and a second one is organization_history. Then I have a table actions_history with concrete action,... But it isn't so important in this case.
In Organization history I keep revision and organizationId. Everything works well until DELETE a table organization. My idea is to keep every action in history table. On INSERT, UPDATE and DELETE action. But problem is when I try to delete organization table. I got this output:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`database`.`base_organizationsHistory`, CONSTRAINT `FK_EAF457A532C8A3DE` FOREIGN KEY (`organization_id`) REFERENCES `base_organizations` (`id`))
1) Is it possible to just delete organization table and keep the history table and ignore foreign key. Or does exist a different and even clear solution?
2) I am thinking also about lifecycle callbacks. In INSERT and UPDATE action I connected tables easily, but when I can create history table on delete action it's not possible to use postRemove callback, because then I don't have the old data for copy to the history. And if I use preRemove callback it's not so clear. Does exist some better idea to do it?
Organization.orm.yml:
BaseBundle\Entity\Organization:
type: entity
table: base_organizations
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 128
type:
type: string
length: 64
oneToMany:
organizationHistory:
targetEntity: OrganizationHistory
mappedBy: organization
nullable: true
lifecycleCallbacks:
postPersist: [saveInsertHistory]
postUpdate: [saveUpdateHistory]
preRemove: [saveDeleteHistory]
and OrganizationHistory.orm.yml
BaseBundle\Entity\OrganizationHistory:
type: entity
table: base_organizationsHistory
uniqueConstraints:
organization_history_idx:
columns: [ organizationId, revision ]
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
name:
type: string
length: 128
type:
type: string
length: 64
revision:
type: integer
nullable: false
organizationId:
type: integer
nullable: false
createdAt:
type: datetime
nullable: false
updatedAt:
type: datetime
nullable: false
manyToOne:
organization:
targetEntity: Organization
inversedBy: organizationHistory
nullable: true
lifecycleCallbacks:
preUpdate: [ setUpdateTimestamp ]
prePersist: [ setCreationTimestamp, setUpdateTimestamp ]
you cannot delete data from one table and also want to store data related to organization in other table. Thats why relations are made for... But, you are using symfony, do "soft delete" which will just mark entity as deleted, but data still will be in your database.
maybe you can start here https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md
I am using Symfony 2.4 and doctrine ORM. I have a parent entity, Property, which has many child relations including:
propertyVideos (OneToMany)
propertyPhotos (OneToMany)
propertyLocation (OneToOne)
In the lastUpdated field of the Property entity, I need to store the date and time of the last update of the Property entity or any of its related entities.
Is there an easy way to do this in Symfony/Doctrine?
Easiest bet would be to use the gedmo/doctrine-extensions or (for Symfony) stof/doctrine-extensions-bundle which you can then use the do the following...
Entity\Article:
type: entity
table: articles
fields:
created:
type: date
gedmo:
timestampable:
on: create // $this create
updated:
type: datetime
gedmo:
timestampable:
on: update // $this update
published:
type: datetime
gedmo:
timestmpable:
on: change
field: type.title
value: Published
// $this->type->title changed to "Published"
blah:
type: datetime
gedmo:
timestampable:
on: change
field: type.somethingelse
// $this->type.somethingelse is changed
manyToOne:
type:
targetEntity: Entity\Type
inversedBy: articles
I've created a new entity in src/Andrei/StatisticsBundle/Entity/Attribute/Value/ButtonVarchar.php. Here is the code for this class:
<?php
namespace Andrei\StatisticsBundle\Entity\Attribute\Value;
class ButtonVarchar
{
protected $value;
}
and in src/Andrei/StatisticsBundle/Resources/config/doctrine/ButtonVarchar.yml I defined the following mapping information:
Andrei\StatisticsBundle\Entity\Attribute\Value\ButtonVarchar:
type: entity
table: button_attribute_value_varchar
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
value:
type: string
length: 255
manyToOne:
button:
targetEntity: Button
inversedBy: attributeValues
joinColumn:
name: button_id
referencedColumnName: id
For some reason when I run php app/console doctrine:generate:entities I get the following error:
[RuntimeException] Namespace "Andrei\StatisticsBundle\Entity\Attribute\Value" does not contain any mapped entities.
I can't understand why is this happening. Can someone point me to the right direction? Thank you.
Did you add your StatisticsBundle to Doctrine config?
eg:
doctrine:
orm:
auto_mapping: true
mappings:
AndreiStatisticsBundle: ~
You can see mapping problem in the following link:
https://github.com/symfony/symfony/pull/675
It seems that your entities are separated into fine grained packages. In that case you need to specify fully qualified namespace in order for it to work.
targetEntity: Fully\Qualified\Namespace\To\Button
It may also help.
This works:
MyUniqBundle:Entity
This doesn't work:
MyUniqBundle/Entity
I'm using a2lix_translation_form tabs in my form. It has the feature, that it allows you to edit several translations to one property in one form. I have it configured like this:
a2lix_translation_form:
locales: [sk, en, de] # [1]
default_required: false ... # further as default
In the form I have following 3 Tabs where I can edit one property (Description)
|SK [Default] | En | DE |
It worked fine (stored things in database and so on), until I turned on the translatable in stof_doctrine_extensions. Here is the config:
stof_doctrine_extensions:
default_locale: sk
orm:
default:
translatable: true # not needed: listeners are not enabled by default
I also use jms_i18n_routing:
jms_i18n_routing:
default_locale: sk
locales: [sk, de, en]
strategy: prefix_except_default
When I acess
localhost/app_dev.php/product/1/edit
then everything looks fine, but when I access
localhost/en/app_dev.php/en/company/11/edit
the Sk [Default] contains En description.
When I set the translatable in stof_doctrine_extensions to false the form is displayed correctly. But I need to have it ON, because I need it for other components. What can I do?
You are in a specific case, that I don't advice henceforth. You will have some difficulties with you database if you change your default locale in the future.
I've updated the doc (I have still some work..), see the end of http://a2lix.fr/bundles/translation-form/#bundle-advanced.
You can use annotation as explaned in the doc or add at the beginning of yours edit/create methods:
$translatableListener = $this->get('stof_doctrine_extensions.listener.translatable');
$translatableListener->setTranslatableLocale($translatableListener->getDefaultLocale());
I'm trying to create a small forum application using Symfony 2 and Doctrine 2. My ForumTopic entity has a last_post field (oneToOne mapping). Now when I persist my new post with
$em->persist($post);
I want to update my ForumTopic entity so its last_post field would reference this new post. I have just realised that it cannot be done with a Doctrine postPersist Listener, so I decided to use a small hack, and tried:
$em->persist($post);
$em->flush();
$topic->setLastPost($post);
$em->persist($post);
$em->flush();
but it doesn't seem to update my topics table.
I also took a look at http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/working-with-associations.html#transitive-persistence-cascade-operations hoping it will solve the problem by adding cascade: [ 'persist' ] to my Topic.orm.yml file, but it didn't help, either.
Could anyone point me to a solution or an example class?
My ForumTopic is:
FrontBundle\Entity\ForumTopic:
type: entity
table: forum_topics
id:
id:
type: integer
generator:
strategy: AUTO
fields:
title:
type: string(100)
nullable: false
slug:
type: string(100)
nullable: false
created_at:
type: datetime
nullable: false
updated_at:
type: datetime
nullable: true
update_reason:
type: text
nullable: true
oneToMany:
posts:
targetEntity: ForumPost
mappedBy: topic
manyToOne:
created_by:
targetEntity: User
inversedBy: articles
nullable: false
updated_by:
targetEntity: User
nullable: true
default: null
topic_group:
targetEntity: ForumTopicGroup
inversedBy: topics
nullable: false
oneToOne:
last_post:
targetEntity: ForumPost
nullable: true
default: null
cascade: [ persist ]
uniqueConstraint:
uniqueSlugByGroup:
columns: [ topic_group, slug ]
And my ForumPost is:
FrontBundle\Entity\ForumPost:
type: entity
table: forum_posts
id:
id:
type: integer
generator:
strategy: AUTO
fields:
created_at:
type: datetime
nullable: false
updated_at:
type: datetime
nullable: true
update_reason:
type: string
nullable: true
text:
type: text
nullable: false
manyToOne:
created_by:
targetEntity: User
inversedBy: forum_posts
nullable: false
updated_by:
targetEntity: User
nullable: true
default: null
topic:
targetEntity: ForumTopic
inversedBy: posts
I believe that you're making this more difficult for yourself, because you think that you have to flush your post before you can set it to an association on your topic.
Doctrine is smart enough that if you persist an entity and set it to an association without first calling flush, it'll make sure that when you do call flush, it persists that entity first, so that it has an ID which can be used with the association.
What this means is that all you really need to do is this:
// Assume that topic has been fetched from the DB, and post is completely new
$topic = $em->find('TopicModel', $topicId);
$post = new ForumPost();
// Fill in the post info
// Set up the association
$topic->setLastPost($post);
// And finally, persist and flush - no more effort needed
$em->persist($post);
$em->flush();
A nice side effect of this is that you can simply use a prePersist event listener to update the thread's last post - Doctrine will take care of everything else for you.
Alternately, if you want an approach that can be a bit easier to follow logically, you can have your Post model call setLastPost on the topic yourself - for instance, if you set the post's topic in your constructor or in a setTopic() method, add the setLastPost call in there.
It's relatively common practice to have one side of an association take care of both sides like this, to help keep things nicely synchronised - see Working with Associations.