Generating data (using PHP) in a YML fixtures file - php

I want to generate test data for a fixture file. I wnat to generate the test data instead of having to type in hundreds of records.
Assuming my schema is as shown below:
foobar_department_def:
_attributes: { phpName: Department }
id:
name: { type: varchar(64), required: true }
foobar_qualification_def:
_attributes: { phpName: Qualification }
id:
name: { type: varchar(64), required: true }
foobar_employee:
_attributes: { phpName: Employee }
id:
first_name: { type: varchar(64), required: true }
last_name: { type: varchar(64), required: true }
biography: { type: longvarchar, required: false }
qualifi_id: { type: integer, foreignTable: foobar_qualification_def, foreignReference: id, required: true, onUpdate: cascade, onDelete: restrict }
dept_id: { type: integer, foreignTable: foobar_department_def, foreignReference: id, required: true, onUpdate: cascade, onDelete: restrict }
_uniques:
idxu_fb_qly_dept: [qualifi_id, dept_id]
How may I generate test data for employees (using PHP in my YML file)?. I saw this being done a little while agao, in the Symfony documentation - however, despite searching again, I can't locate the page on the SF website (maybe its been removed?)

I guess you are looking for dynamic fixtures. And really keep this in mind:
The <?php ?> statements must always start the line or be embedded in a value.
If a <?php ?> statement ends a line, you need to explicly output a new line ("\n").

Symfony used to (and probably still does) allow PHP in it's configuration YML files but I'm not sure about the fixtures files. However, for what it sounds like you're trying to do PHP in the YML file won't be necessary, you just need to create a script that writes a giant YML file once.
To generate your fixtures I would suggest creating one row in each of the tables you have defined and then dumping that data into a fixtures file to give a template for your data. Then use that fixtures template for your PHP script, replacing your test values with generated data in a loop.

Related

Invalid row key specified, data-load fixtures error Symfony 1.4

I have a problem with a symfony 1.4 setup :
I created the schema.yml:
BlogCategory:
actAs: { Timestampable: ~ }
columns:
name: { type: string(255), notnull: true, unique: true }
BlogPost:
actAs: { Timestampable: ~ }
columns:
category_id: { type: integer, notnull: true }
title: { type: string(255), notnull: true }
body: { type: string(255), notnull: true }
relations:
BlogCategory: { onDelete: CASCADE, local: category_id, foreign: id, foreignAlias: BlogPosts }
(based on jobeet tutorial )
then it generated me the table schema.sql :
CREATE TABLE blog_category (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255) NOT NULL UNIQUE, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL);
CREATE TABLE blog_post (id INTEGER PRIMARY KEY AUTOINCREMENT, category_id INTEGER NOT NULL, title VARCHAR(255) NOT NULL, body VARCHAR(255) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL);
which seems right to me.
And to finish I tried to add fixtures and load the data, my fixtures are as follows:
data/fixtures/categories.yml:
BlogCategory:
design:
name: Design
programming:
name: Programming
management:
name: Management
administrator :
name: Administrator
data/fixtures/posts.yml
BlogPost:
initial_post_1:
BlogCategory : design
title: Initial post 1
body: This post is an initial test number 1
initial_post_2:
BlogCategory : design
title: Initial post 2
body: This post is an initial test number 2
The php symfony doctrine:data-load command is generating me the categories but not the posts, so I tried :
php symfony doctrine:data-load data/fixtures/posts.yml
Invalid row key specified: (blog_category) design, referred to in
(blog_post) initial_post_1
Any idea why I can't laod the posts ? I've already tried to delete the db re-generate etc...
Okey so after many tests and git reset --hard HEAD to fix my datas, it seems that I had a problem with my BlogPost.class.php which contained an empty save() method override that was messing things up.
Plus, to rewrite data in the right way the command to use is:
php symfony doctrine:build --all --and-load
and not :
php symfony doctrine:data-load
Hope it could help anyone with the same problem !

Cascaded persist not working (Doctrine ORM + Symfony 2)

I started working with symfony several months ago and there is one thing keeps bothering me all the time. It is when I have a one-to-many relationship in Doctrine and I try to insert something into the database. Here's an example:
Broker.orm.yml
Acme\DemoBundle\Entity\Broker:
type: entity
table: brokers
repositoryClass: BrokerRepository
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
slug:
type: string
length: 64
oneToMany:
accountTypes:
targetEntity: Acme\DemoBundle\Entity\AccountType
mappedBy: broker
cascade: ["persist"]
AccountType.orm.yml
Acme\DemoBundle\Entity\AccountType:
type: entity
table: account_types
repositoryClass: AccountTypeRepository
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
slug:
type: string
length: 64
manyToOne:
broker:
targetEntity: Acme\DemoBundle\Entity\Broker
inversedBy: accountTypes
joinColumn:
name: broker_id
referencedColumn: id
Then is try to save it to the database like this.
$accountType = new AccountType();
$accountType->setName("blabla");
// additional data to accountType
$broker->addAccountType($accountType);
$em->persist($broker);
$em->flush();
The strange thing is that it works prefrectly with only one tiny problem. Broker is updated, and AccountType is inserted into the database but the accountType doesn't have any relations with the Broker. In other words, when I check in the database the broker_id fields reamains untouched and contains NULL.
If I add $accountType->setBroker($broker) manually, it works. But I started to use Sonata Admin Bundle where there is much more complicated to do this and I don't really need a complex admin system. So I just want a fast development on it and without this "feature" it's nearly impossible.
And anyways, if I add something to a collection of an Object, it should know which object is its parent, right? :)
Thanks for your help in advance!
class Broker
{
public function addAccountType($accountType)
{
$this->accountTypes[] = $accountType;
// *** This is what you are missing ***
$accountType->setBroker($this);

Use of "Container" as model name in Symfony

I have a entity called Container in a Symfony application, which I have included in the schema.yml file:
Container:
columns:
id: { type: integer, primary: true, autoincrement: true }
name: { type: string(127), notnull: true }
Strain:
columns:
id: { type: integer, primary: true, autoincrement: true }
...
container_id: { type: integer }
...
relations:
Container: { foreignAlias: Strains }
Then I have regenerated the models, forms and filters using the symfony doctrine:build --all-classes task.
Now when I try to use $strain->getContainer(), e.g. in a showSuccess action, it returns no object at all. I have double-checked that container_id has a reference to a record in container table.
Moreover, when I try to edit a Strain object and unlink the relationship with Container, the form is saved correctly, but the container_id column keeps the old value.
Do you know if Container is a reserved word or something like that in Symfony or Doctrine? What can be happening?
Thanks!
No, it isn't a reserved word.
(In fact, I have a model in my project with a Container relation).
Have you tried setting the local property on Container relation to container_id?

short timestamp in symfony 1.4 and doctrine 1.2

I generated new project in Symfony and I used Doctrine:
expires_at: { type: timestamp, notnull: true }
but it is too long for me. I need only 2011/06/05. How can I achieve this? The widget in the form is asking for too much. One must also fill in for example milliseconds.
This:
unset(
$this['expires_at']
);
deleted all field in the widget. I would like something like that remained 2011/06/05.
doctrine has a special datatype for this purpose : http://www.doctrine-project.org/documentation/manual/1_2/pl/defining-models:columns:data-types#date
Put this in your schema.yml :
expires_at: { type: date, notnull: true }
and regenerate your form.

PHP Doctrine Validation

I'm using doctrine and trying to validate some models.
I have the following in my YAML schema:
User:
package: User
columns:
username:
type: string
notnull: true
notblank: true
minlength: 4
password:
type: string
notnull: true
If I create a new user, it always validates, regardless of what values I give it.
eg:
$testuser = new User();
$testuser->username = ' ';
if ( ! $testuser->isValid())
{
echo 'User is invalid!';
}
EDIT:
The above is just an example. It still validates even if values specified as NOT NULL in the schema are omitted.
The invalid method is never produced. Does anyone know what might be causing this?
Any advice appreciated.
Thanks.
the reason is: there's no isValid() function in your models which created by Doctrine. (in your models/generated/*.php)
Step 1.
refer to Doctrine Manual: you should put this in your bootstrap.php or any your php file header)
$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
Step 2.
rebuild your models' files.
Step 3.
it should work now :)

Categories