Can a table have multiple slugs in Doctrine? - php

Is it possible to have multiple slugs on one table in Doctrine?
I tried this in my yaml-file:
Article:
tableName: tst_article
actAs:
Sluggable:
unique: true
fields: [title]
canUpdate: true
Sluggable:
unique: true
fields: [text]
name: secondSlug
columns:
id:
type: integer(8)
primary: true
autoincrement: true
category_id:
type: integer(8)
title:
type: text(255)
text:
type: clob
But after generating the sql only the secondSlug was generated...

It is possible. In your table definition write:
public function setUp() {
parent::setUp();
$sluggable0 = new Doctrine_Template_Sluggable(array(
'fields' => array(0 => 'name'),
'unique' => true,
'canUpdate' => true
));
$this->actAs($sluggable0);
$sluggable1 = new Doctrine_Template_Sluggable(array(
'fields' => array(0 => 'native_name'),
'unique' => false,
'canUpdate' => false,
'name' => 'native_name_slug'
));
$this->actAs($sluggable1);
}
The problem is in YAML itself. You have something like this:
keyA:
keyB: value
keyB: value
What might be translated into:
array(
'keyA' => array(
'keyB' => 'value',
'keyB' => 'value'
)
);
So as you see there is definition of keyB and then keyB is overwritten with new value. So in your YAML file second definition overrides the first one.
How to solve that? I don't know, but I'll do some researches. Right now you're forced to declare your models in pure PHP.

Article:
tableName: tst_article
actAs:
Sluggable:
unique: true
fields: [title, text]
canUpdate: true
columns:
id:
type: integer(8)
primary: true
autoincrement: true
category_id:
type: integer(8)
title:
type: text(255)
text:
type: clob

Although changing libraries is not recommended, sometimes it's a necessary evil. A very small change allows you to declare Sluggable_1, Sluggable_2, etc. in your YAML.
--- a/lib/vendor/doctrine/Doctrine/Import/Builder.php
+++ b/lib/vendor/doctrine/Doctrine/Import/Builder.php
## -711,8 +711,10 ## class Doctrine_Import_Builder extends Doctrine_Builder
{
// find class matching $name
$classname = $name;
- if (class_exists("Doctrine_Template_$name", true)) {
- $classname = "Doctrine_Template_$name";
+ // HACK to allow multiple Sluggables
+ $classname = preg_replace('/_[0-9]+$/', '', $classname);
+ if (class_exists("Doctrine_Template_$classname", true)) {
+ $classname = "Doctrine_Template_$classname";
}
return " \$" . strtolower($name) . "$level = new $classname($option);". PHP_EOL;
}

Related

dump function ignore inline parameter when using TaggedValue

I have an issue with Yaml::Dump() function.
when I use TaggedValue, dump function is ignoring parameter $inline
If I'm not using TaggedValues, it's working as expected.
This is example code used
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Tag\TaggedValue;
$Admins = array(
array(
'samaccountname' => ['user1'],
'cn' => ['Louisa Nicolas'],
'mail' => ['mail#domain.com'],
),
array(
'samaccountname' => ['user2'],
'cn' => ['Telper Max'],
'mail' => ['mail#domain.com'],
)
);
$rootYaml = array();
foreach ($Admins as $Admin) {
$adm = array(
'id' => $Admin['samaccountname'][0],
'annotations' => array(
'name' => $Admin['cn'][0],
'email' => $Admin['mail'][0]
));
$rootYaml[] = $adm;
$rootYamlTagged[] = new TaggedValue('user', $adm);
}
echo "##### Works without tags\n";
print Yaml::dump($rootYaml, 3);
echo "##### not working with tags\n";
print Yaml::dump($rootYamlTagged, 3);
Result :
##### Works without tags
-
id: user1
annotations:
name: 'Louisa Nicolas'
email: mail#domain.com
-
id: user2
annotations:
name: 'Telper Max'
email: mail#domain.com
##### not working with tags
- !user { id: user1, annotations: { name: 'Louisa Nicolas', email: mail#domain.com } }
- !user { id: user2, annotations: { name: 'Telper Max', email: mail#domain.com } }
I would expect to have for tagged
- !user
id: user1,
annotations:
name: 'Louisa Nicolas'
email: mail#domain.com
- !user
id: user2
annotations:
name: 'Telper Max'
email: mail#domain.com
I opened a bug for that on support team, and bug was confirmed.
Yaml dump : ignore inline parameter when TaggedValue used
Bug has been approved.
It's fixed in version 3.4.
The pull request [Yaml] fix inline handling when dumping tagged values has the actual code changes.

Symfony2 associative array in entity

i have entity languages with format
id; name
en; English
pl; Polish
etc...
And I have some Entity translations, that in one column has an associative array:
{pl: "Some txt in PL", en: "Some txt in EN",...}
Everything is almost perfect, but i don't know how to create an form for editing such a thing :D I tried almost everything.
Translation.orm.yml:
...
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
value:
type: array
...
For new Entity you must prepare your array in Controller ( before creating form). Something like that:
$value = array('en' => 'here en', 'pl' => 'here pl');
$translation->setValue($value);
$form = $this->createForm(new TranslationType()...
In Your FormType:
$builder->add('value', 'collection', array(
'type' => 'text',
'options' => array(
'required' => true,
),
)
);
That's all.
If you have {{ form_rest(form) }} in your twig, you'll see two additional fields for translations in your form and it will be work.
Additional information about collection field type is here

drop down problems using symfony

i am new in symfony,its has been two days i have stacked on a problems making drop downs using symfony froms relations based on schema.yml
schema.yml is
FieldsCategory:
tableName: fld_category
columns:
id:
name: id as FieldsId
type: integer(10)
primary: true
autoincrement: true
category_name:
name: category_name as CategoryName
type: string
default: ''
notnull: true
relations:
FieldsCategory:
class: FieldsCategory
foreignAlias: Fields
local: id
foreign: fields_category_id
Fields:
tableName: fld_fields
columns:
id:
name: id as FieldsId
type: integer(10)
primary: true
autoincrement: true
fields_name:
name: fields_name as FieldsName
type: string
default: ''
notnull: true
fields_desc:
name: fields_desc as FieldsDescription
type: string
default: ''
notnull: true
amount:
name: amount as TotalAmount
type: integer
notnull: true
fields_type_id:
name: fields_type_id as FieldsTypeId
type: integer
fields_category_id:
name: fields_category_id as FieldsCategoryId
type: integer
and my forms is
$this->setWidgets(array(
'FieldsId' => new sfWidgetFormInputHidden(),
'fields_type_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Fields'), 'add_empty' => false)),
'FieldsName' => new sfWidgetFormTextarea(),
));
$this->setValidators(array(
'FieldsId' => new sfValidatorChoice(array('choices' => array($this->getObject()->get('FieldsId')), 'empty_value' => $this->getObject()->get('FieldsId'), 'required' => false)),
'fields_type_id' => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Fields'), 'required' => false)),
'FieldsName' => new sfValidatorString(array('required' => false)),
));
its given me this error
i have tried all possible ways but i couldnt solve this problems/
and help will be approciated in advance

Symfony 1.4 and doctrine autoincrement instead of auto_increment

I have a weird problem.
Here's my yaml:
Request:
actAs: { Timestampable: ~ }
columns:
id: { type: integer(4), primary: true, autoincrement: true, notnull: true }
When i perform a symfony doctrine:build --all --no-confirmation inside my BaseRequest.class.php there's an error during the declaration of my id field:
$this->hasColumn('id', 'integer', 4, array(
'type' => 'integer',
'primary' => true,
'autoincrement' => true,
'length' => 4,
));
As you can see it should be auto_increment but instead it's autoincrement so i have to manually change this after each build.
Is there any way to fix this ?
not exactly answer to your question, but you don't need to specify id column for your entities, doctrine does that automatically

Doctrine case sensitivity in schema.yml

Doctrine turns my column names into all lower case to improve compatibility. How do I prevent this from happening?
users:
actAs: [Timestampable]
columns:
userId:
type: integer
length: 4
primary: true
autoincrement: true
or
$this->hasColumn('userId', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'primary' => true,
'autoincrement' => true,
));
then becomes
userid
This is a problem because I have lots of existing code and data that uses the camelCase convention. Is there some sort of easy boolean I can change to make it keep my columns exactly as written?
The Yaml conversion is where the issue lies. To fix this you need to alias the column name in the YAML schema file IE: name: user_id as userId

Categories