short timestamp in symfony 1.4 and doctrine 1.2 - php

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.

Related

How to add custom properties to columns in doctrine 1.2 schema file

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.

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?

PHPs Doctrine creates empty records

I played a lot with Doctrine 1.2. Creating and deleting records are no problem anymore ;). But sometimes i have empty records in my database. Every field is set to NULL. I have the feeling it has something to do with relations. How can i prevent Doctrine of creating such empty entries.
In your schema use the tag notnull: true to force non-empty fields
and use primary: true for id's
i.e.:
table:
columns:
id:
primary: true
unsigned: true
type: integer(4)
autoincrement: true
field:
type: ...
notnull: true
I this does not help you, please put further information
This should be a Problem in Code, Doctrine itself does not create empty records. I believe you save somewhere a null filled model.
Be carefull with notnull:true as it leads to incompatibilities with Oracle, if you don't solve above Problem.
I have only found this somewhat hackish solution so far, to be inserted for each related field for which a null values should be possible.
public function preSave($trigger) {
// Avoid empty relations
if(!$this->getRelatedobjectId())
$this->setRelatedobject(null);
}
}

Generating data (using PHP) in a YML fixtures file

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.

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