PHP Doctrine Validation - php

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 :)

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.

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.

Doctrine Error "Invalid expression found"

I have a table defined by:
Modification:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string
author: string
attributes:
export: all
When I run this code:
$tbl = new Doctrine_Table(
'Modifications',
Doctrine_Manager::getInstance()
->getCurrentConnection()
);
$tbl->findOneBy('name', 'yacoby');
I get this error:
Doctrine_Table_Exception: Invalid expression found: ()n()a()m()e()
/home/yacoby/documents/dev/netbeans/php/Doctrine/Table.php:2741
/home/yacoby/documents/dev/netbeans/php/Doctrine/Table.php:1707
/home/yacoby/documents/dev/netbeans/ESSearch/test/library/Search/Table/ModsTest.php:21
Does anyone have any idea why and what I can do to fix it?
Try deleting the 's' after "Modification"...
If that doesn't have any affect, you may want to check that the table has been constructed properly by calling something like the method Doctrine_Table::getColumns and checking the output (i.e. that the array contains the columns you defined).
If it isn't what you expect, check that the files that the models are defined in have been loaded properly.
If the table has been set up already somehow, the correct way to get it is like this:
$tbl = Doctrine::getTable($what);
I ran into the same error while trying to construct (while I should have been getting) the Doctrine_Table of the $what entity to run som magic find functions on it.

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);
}
}

Problems with database relations with symfony framework - Cannot fetch TableMap for undefined table

Currently developing an application using the newest version of symfony, obtained through PEAR. This is my exact schema configuration
propel:
user:
id:
name: { type: varchar(255), required: true }
level: { type: integer, required: true, default: 1 }
created_at:
post:
id:
title: { type: varchar(255), required: true }
post: { type: longvarchar, required: true }
user_id:
created_at:
updated_at:
comment:
id:
relation: integer
comment: { type: varchar(300), required: true }
nick: { type: varchar(100), required: true }
created_at:
updated_at:
The awake and aware of you have probably noticed that user_id in post is a foreign key to user. According to the definite guide; [quote title=Quote:](columns ending with _id are considered to be foreign keys, and the related table is automatically determined according to the first part of the column name).[/quote]
Whenever I try to build EITHER filters or FORMS (build-filters/build-forms/build-all) I get the follwing error message.
>> propel generating form classes
-----> Cannot fetch TableMap for undefined table: user. Make sure you have the static MapBuilder registration code after your peer stub class definition. <------
[?php
/**
* Post form base class.
*
* #package ##PROJECT_NAME##
* #subpackage form
* #author ##AUTHOR_NAME##
* #version SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
*/
class BasePostForm extends BaseFormPropel
{
public function setup()
{
$this->setWidgets(array(
'id' => new sfWidgetFormInputHidden(),
'title' => new sfWidgetFormInput(),
'post' => new sfWidgetFormTextarea(),
'user_id' => new sfWidgetFormPropelChoice(
I've also tried defining the foreignTable and foreignReference in my schema, but without luck. This is a fresh symfony-project, only created to debug this error. I've tried using singular and plural names for my tables. Am I doing something horribly wrong, or is this a solid bug? Appreciate ANY input, this is driving me crazy!
I have a forum thread over at symfony's official community, here: http://forum.symfony-project.org/index.php/m/77979/ - there's been some suggestions (like how I forgot to use a tilde and that "user" is a pre-defined class, but the problems remains SOLVED [edited] as of now.
One free internet hug, major props and a free space shuttle to the mighty soul who come up with a solution! (disclaimer: there's actually no free space shuttle)
Update: PROBLEM HAS BEEN SOLVED! ... after countless hours of back tracking and debugging.
The issue was caused by a TABLE prefix manually coded in in propel.ini like this:
; custom table prefix
propel.tablePrefix = mindmonkey_
There's no communication with schema.yml and the propel.ini or something like that, so everything bugged up. My conclusion from this is: forget prefixes, just use a dedicated schema for each project... I just used prefix as a habit, always done - but never again unless specifically instructed to do so!

Categories