Why does propel fail to set this ENUM field to "Y"?

173 Views Asked by At

I'm scratching my head over this. I have the following table:

<table name="activity" idMethod="native" phpName="Activity">
  <column name="id" phpName="Id" type="INTEGER" size="13" primaryKey="true" autoIncrement="true" required="true"/>
  <column name="created" phpName="Created" type="TIMESTAMP" required="true" defaultExpr="CURRENT_TIMESTAMP"/>
  <column name="modified" phpName="Modified" type="TIMESTAMP" required="true" defaultExpr="CURRENT_TIMESTAMP"/>
  <column name="provider_id" phpName="ProviderId" type="INTEGER" size="13" required="true"/>
  <column name="name" phpName="Name" type="VARCHAR" size="100" required="true"/>
  <column name="description" phpName="Description" type="LONGVARCHAR"/>
  <column name="grade_min" phpName="GradeMin" type="INTEGER" size="2" required="true" defaultValue="0"/>
  <column name="grade_max" phpName="GradeMax" type="INTEGER" size="2" required="true" defaultValue="12"/>
  <column name="duration" phpName="Duration" type="INTEGER" size="3" required="true" defaultValue="60"/>
  <column name="image" phpName="Image" type="VARCHAR" size="255" required="true" defaultValue=""/>
  <column name="webpage" phpName="Webpage" type="VARCHAR" size="255" required="true" defaultValue=""/>
  <column name="approved" phpName="Approved" type="CHAR" sqlType="enum('Y','N')" required="true" defaultValue="N"/>
  <foreign-key foreignTable="provider" name="activity_provider" onUpdate="CASCADE">
    <reference local="provider_id" foreign="id"/>
  </foreign-key>
  <unique name="provider_name">
    <unique-column name="provider_id"/>
    <unique-column name="name"/>
  </unique>
  <vendor type="mysql">
    <parameter name="Engine" value="InnoDB"/>
  </vendor>
</table>

And I execute the following PHP:

$approved = $activity->getApproved();
if($approved == "N"){
  $activity->setApproved("Y"); // verified this line is executing
} else {
  $activity->setApproved("N");
}

echo $activity->getApproved(); // this outputs "Y"
$activity->save(); // produces line in the query log
die($activity->getApproved()); // this outputs "N"

A line is produced in the query log from the save() method:

[2018-02-26T17:00:09.038703-08:00] default.INFO: UPDATE activity SET approved='N' WHERE activity.id=6 [] []

When the approved field is set to Y, the same query runs and is successful (the field is toggled to N). Any ideas what might be going on here? Does Propel2 hate ENUM? Any suggestions to troubleshoot this issue?

1

There are 1 best solutions below

0
Zeal On

Sorry, I found the cause of the issue I was having. I had a preSave hook on the Activity model that set approved to "N" on save. The intention was that if the activity was changed it would lose its approved status, but because saving happens after setApproved("Y"), the hook fires and sets it to "N". I fixed it by detecting whether the Approved column was being modified and only setting the Approved column to "N" if it was not being modified.