0

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?

Zeal
  • 474
  • 3
  • 4
  • Try with ``. My guess is that `CHAR` is messing things up and it defaults to `N` as specified. – ishegg Feb 27 '18 at 02:27
  • @ishegg thanks for the `valueSet` idea. Even though my issue wasn't related to `enum`, that is useful knowledge. Apparently propel's `ENUM` columns are backed by `integer` columns in the database, so it doesn't work in my case (dealing with a populated reversed database, so I don't want to switch the `enum` field to `integer`), but it would appear that `valueSet` is working with `CHAR`. Or at the very least `propel model:build` is not complaining. – Zeal Feb 27 '18 at 16:55

1 Answers1

0

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.

Zeal
  • 474
  • 3
  • 4