propelorm/Propel

deleted flag conflicts with deleted column in a table

Closed this issue · 6 comments

In database first scenario:
Scenario: if you have table which contains a deleted column (ex: with type datetime) and then try generate schema.xml and after that generate models.
Problem: In generated om base class there is a deleted field which is used to mark that the entity has been deleted (method setDeleted, isDeleted).

public function isDeleted()
{
    return $this->_deleted;
}
 public function setDeleted($b)
{
    $this->_deleted = (boolean) $b;

    return $this;
}

`
When deleted column is detected by the generator it will try to generate setDeleted again but for deleted column.

public function setDeleted($v)
   {
        $dt = PropelDateTime::newInstance($v, null, 'DateTime');
        if ($this->deleted !== null || $dt !== null) {
            $currentDateAsString = ($this->deleted !== null && $tmpDt = new DateTime($this->deleted)) ? $tmpDt->format('Y-m-d H:i:s') : null;
            $newDateAsString = $dt ? $dt->format('Y-m-d H:i:s') : null;
            if ($currentDateAsString !== $newDateAsString) {
                $this->deleted = $newDateAsString;
                $this->modifiedColumns[] = SeriesPeer::DELETED;
            }
        } // if either are not null


        return $this;
    } // setDeleted()

when you delete an entity it will try to call setDeleted(true) and an error will occur because method setDeleted has been rewritten by the generator because of deleted column from db.

try {
            $deleteQuery = SeriesQuery::create()
                ->filterByPrimaryKey($this->getPrimaryKey());
            $ret = $this->preDelete($con);
            if ($ret) {
                $deleteQuery->delete($con);
                $this->postDelete($con);
                $con->commit();
                **$this->setDeleted(true);**
            } else {
                $con->commit();
            }

As a workaround I can modify the schema.xml and locate the "deleted" column and change the phpName is something else but in my case it doesn't help me because we have shared db and the migrations are executed from a different application. This means that every time I regenerate the schema.xml I have to modify again the phpNames for deleted columns.

marcj commented

This is an already known limitation which couldn't be easily fixed. This is fixed with the new data-mapper architecture in data-mapper in v2 branch.

I've tried the same thing in propel2 with the default configurations and I had almost the same problem.
In the base Class I saw two fields with the same name.

/**
     * attribute to determine whether this object has been deleted.
     * @var boolean
     */
    protected $deleted = false;

 /**
     * The value for the deleted field.
     *
     * @var        DateTime
     */
    protected $deleted;

Can you please give me a hint from where I should configure the data-mapper ?
Thanks

The problem has been solved by integrating a custom behavior. Anyway thanks for your help,

@alexandrubeu could you explain how did you tackle this issue? (without pointing to data-mapper branch I suppose)

@marcj I was wondering when it would be available in production?

marcj commented

https://github.com/propelorm/Propel3 - when people help finding bugs, it can be used sooner in production.