Skip to content

Commit

Permalink
Debug (#16)
Browse files Browse the repository at this point in the history
Fix error with mailjet ID
  • Loading branch information
Lurivar authored and lopes-vincent committed Nov 18, 2019
1 parent d6a82eb commit 07cee38
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 38 deletions.
4 changes: 2 additions & 2 deletions Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.2</version>
<version>1.3</version>
<author>
<name>Benjamin Perche, Franck Allimant</name>
<email>bperche@openstudio.fr, thelia@cqfdev.fr</email>
</author>
<type>classic</type>
<thelia>2.2.1</thelia>
<thelia>2.3.4</thelia>
<stability>other</stability>
</module>
5 changes: 3 additions & 2 deletions Config/schema.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<database defaultIdMethod="native" name="thelia">
<table name="mailjet_newsletter" namespace="Mailjet\Model">
<column name="id" type="INTEGER" required="true" primaryKey="true" />
<column name="id" autoIncrement="true" type="INTEGER" required="true" primaryKey="true" />
<column name="mailjet_id" type="VARCHAR" size="255" required="true" />
<column name="email" type="VARCHAR" size="255" required="true" />
<column name="relation_id" type="INTEGER" required="true" />
<column name="relation_id" type="INTEGER"/>

<index name="idx_mailjet_newsletter_email">
<index-column name="email" />
Expand Down
2 changes: 2 additions & 0 deletions Config/sqldb.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Sqlfile -> Database map
thelia.sql=thelia
9 changes: 6 additions & 3 deletions Config/thelia.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ SET FOREIGN_KEY_CHECKS = 0;
-- mailjet_newsletter
-- ---------------------------------------------------------------------

CREATE TABLE IF NOT EXISTS `mailjet_newsletter`
DROP TABLE IF EXISTS `mailjet_newsletter`;

CREATE TABLE `mailjet_newsletter`
(
`id` INTEGER NOT NULL,
`id` INTEGER NOT NULL AUTO_INCREMENT,
`mailjet_id` VARCHAR(255) NOT NULL,
`email` VARCHAR(255) NOT NULL,
`relation_id` INTEGER NOT NULL,
`relation_id` INTEGER,
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email`),
UNIQUE INDEX `relation_id_UNIQUE` (`relation_id`),
Expand Down
22 changes: 22 additions & 0 deletions Config/update/1.3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is a fix for InnoDB in MySQL >= 4.1.x
# It "suspends judgement" for fkey relationships until are tables are set.
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
-- mailjet_newsletter
-- ---------------------------------------------------------------------

ALTER TABLE `mailjet_newsletter` MODIFY `relation_id` INT;

ALTER TABLE `mailjet_newsletter` MODIFY `id` VARCHAR(255);

ALTER TABLE `mailjet_newsletter` CHANGE `id` `mailjet_id` VARCHAR(255);

ALTER TABLE `mailjet_newsletter` ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT UNIQUE FIRST;

ALTER TABLE `mailjet_newsletter`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`id`);

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
8 changes: 4 additions & 4 deletions EventListeners/NewsletterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function update(NewsletterEvent $event)
) {
// Reset relation ID.
$model
->setRelationId(0)
->setRelationId(null)
->save();
/**
* Then create a new client
Expand Down Expand Up @@ -123,8 +123,8 @@ protected function apiAddContactList(NewsletterEvent $event, MailjetNewsletter $
"IsUnsubscribed" => "False",
];

if (intval($model->getRelationId()) == 0) {
$params["ContactID"] = $model->getId();
if (($model->getRelationId()) === null) {
$params["ContactID"] = $model->getMailjetId();
$params["ListALT"] = ConfigQuery::read(MailjetModule::CONFIG_NEWSLETTER_LIST);

// Add the contact to the contact list
Expand Down Expand Up @@ -189,7 +189,7 @@ protected function apiAddUser(NewsletterEvent $event, $function)

$model = new MailjetNewsletter();
$model
->setId($data["Data"][0]["ID"])
->setMailjetId($data["Data"][0]["ID"])
->setEmail($event->getEmail())
->save();
}
Expand Down
89 changes: 79 additions & 10 deletions Model/Base/MailjetNewsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ abstract class MailjetNewsletter implements ActiveRecordInterface
*/
protected $id;

/**
* The value for the mailjet_id field.
* @var string
*/
protected $mailjet_id;

/**
* The value for the email field.
* @var string
Expand Down Expand Up @@ -346,6 +352,17 @@ public function getId()
return $this->id;
}

/**
* Get the [mailjet_id] column value.
*
* @return string
*/
public function getMailjetId()
{

return $this->mailjet_id;
}

/**
* Get the [email] column value.
*
Expand Down Expand Up @@ -389,6 +406,27 @@ public function setId($v)
return $this;
} // setId()

/**
* Set the value of [mailjet_id] column.
*
* @param string $v new value
* @return \Mailjet\Model\MailjetNewsletter The current object (for fluent API support)
*/
public function setMailjetId($v)
{
if ($v !== null) {
$v = (string) $v;
}

if ($this->mailjet_id !== $v) {
$this->mailjet_id = $v;
$this->modifiedColumns[MailjetNewsletterTableMap::MAILJET_ID] = true;
}


return $this;
} // setMailjetId()

/**
* Set the value of [email] column.
*
Expand Down Expand Up @@ -471,10 +509,13 @@ public function hydrate($row, $startcol = 0, $rehydrate = false, $indexType = Ta
$col = $row[TableMap::TYPE_NUM == $indexType ? 0 + $startcol : MailjetNewsletterTableMap::translateFieldName('Id', TableMap::TYPE_PHPNAME, $indexType)];
$this->id = (null !== $col) ? (int) $col : null;

$col = $row[TableMap::TYPE_NUM == $indexType ? 1 + $startcol : MailjetNewsletterTableMap::translateFieldName('Email', TableMap::TYPE_PHPNAME, $indexType)];
$col = $row[TableMap::TYPE_NUM == $indexType ? 1 + $startcol : MailjetNewsletterTableMap::translateFieldName('MailjetId', TableMap::TYPE_PHPNAME, $indexType)];
$this->mailjet_id = (null !== $col) ? (string) $col : null;

$col = $row[TableMap::TYPE_NUM == $indexType ? 2 + $startcol : MailjetNewsletterTableMap::translateFieldName('Email', TableMap::TYPE_PHPNAME, $indexType)];
$this->email = (null !== $col) ? (string) $col : null;

$col = $row[TableMap::TYPE_NUM == $indexType ? 2 + $startcol : MailjetNewsletterTableMap::translateFieldName('RelationId', TableMap::TYPE_PHPNAME, $indexType)];
$col = $row[TableMap::TYPE_NUM == $indexType ? 3 + $startcol : MailjetNewsletterTableMap::translateFieldName('RelationId', TableMap::TYPE_PHPNAME, $indexType)];
$this->relation_id = (null !== $col) ? (int) $col : null;
$this->resetModified();

Expand All @@ -484,7 +525,7 @@ public function hydrate($row, $startcol = 0, $rehydrate = false, $indexType = Ta
$this->ensureConsistency();
}

return $startcol + 3; // 3 = MailjetNewsletterTableMap::NUM_HYDRATE_COLUMNS.
return $startcol + 4; // 4 = MailjetNewsletterTableMap::NUM_HYDRATE_COLUMNS.

} catch (Exception $e) {
throw new PropelException("Error populating \Mailjet\Model\MailjetNewsletter object", 0, $e);
Expand Down Expand Up @@ -687,11 +728,18 @@ protected function doInsert(ConnectionInterface $con)
$modifiedColumns = array();
$index = 0;

$this->modifiedColumns[MailjetNewsletterTableMap::ID] = true;
if (null !== $this->id) {
throw new PropelException('Cannot insert a value for auto-increment primary key (' . MailjetNewsletterTableMap::ID . ')');
}

// check the columns in natural order for more readable SQL queries
if ($this->isColumnModified(MailjetNewsletterTableMap::ID)) {
$modifiedColumns[':p' . $index++] = 'ID';
}
if ($this->isColumnModified(MailjetNewsletterTableMap::MAILJET_ID)) {
$modifiedColumns[':p' . $index++] = 'MAILJET_ID';
}
if ($this->isColumnModified(MailjetNewsletterTableMap::EMAIL)) {
$modifiedColumns[':p' . $index++] = 'EMAIL';
}
Expand All @@ -712,6 +760,9 @@ protected function doInsert(ConnectionInterface $con)
case 'ID':
$stmt->bindValue($identifier, $this->id, PDO::PARAM_INT);
break;
case 'MAILJET_ID':
$stmt->bindValue($identifier, $this->mailjet_id, PDO::PARAM_STR);
break;
case 'EMAIL':
$stmt->bindValue($identifier, $this->email, PDO::PARAM_STR);
break;
Expand All @@ -726,6 +777,13 @@ protected function doInsert(ConnectionInterface $con)
throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e);
}

try {
$pk = $con->lastInsertId();
} catch (Exception $e) {
throw new PropelException('Unable to get autoincrement id.', 0, $e);
}
$this->setId($pk);

$this->setNew(false);
}

Expand Down Expand Up @@ -777,9 +835,12 @@ public function getByPosition($pos)
return $this->getId();
break;
case 1:
return $this->getEmail();
return $this->getMailjetId();
break;
case 2:
return $this->getEmail();
break;
case 3:
return $this->getRelationId();
break;
default:
Expand Down Expand Up @@ -811,8 +872,9 @@ public function toArray($keyType = TableMap::TYPE_PHPNAME, $includeLazyLoadColum
$keys = MailjetNewsletterTableMap::getFieldNames($keyType);
$result = array(
$keys[0] => $this->getId(),
$keys[1] => $this->getEmail(),
$keys[2] => $this->getRelationId(),
$keys[1] => $this->getMailjetId(),
$keys[2] => $this->getEmail(),
$keys[3] => $this->getRelationId(),
);
$virtualColumns = $this->virtualColumns;
foreach ($virtualColumns as $key => $virtualColumn) {
Expand Down Expand Up @@ -856,9 +918,12 @@ public function setByPosition($pos, $value)
$this->setId($value);
break;
case 1:
$this->setEmail($value);
$this->setMailjetId($value);
break;
case 2:
$this->setEmail($value);
break;
case 3:
$this->setRelationId($value);
break;
} // switch()
Expand Down Expand Up @@ -886,8 +951,9 @@ public function fromArray($arr, $keyType = TableMap::TYPE_PHPNAME)
$keys = MailjetNewsletterTableMap::getFieldNames($keyType);

if (array_key_exists($keys[0], $arr)) $this->setId($arr[$keys[0]]);
if (array_key_exists($keys[1], $arr)) $this->setEmail($arr[$keys[1]]);
if (array_key_exists($keys[2], $arr)) $this->setRelationId($arr[$keys[2]]);
if (array_key_exists($keys[1], $arr)) $this->setMailjetId($arr[$keys[1]]);
if (array_key_exists($keys[2], $arr)) $this->setEmail($arr[$keys[2]]);
if (array_key_exists($keys[3], $arr)) $this->setRelationId($arr[$keys[3]]);
}

/**
Expand All @@ -900,6 +966,7 @@ public function buildCriteria()
$criteria = new Criteria(MailjetNewsletterTableMap::DATABASE_NAME);

if ($this->isColumnModified(MailjetNewsletterTableMap::ID)) $criteria->add(MailjetNewsletterTableMap::ID, $this->id);
if ($this->isColumnModified(MailjetNewsletterTableMap::MAILJET_ID)) $criteria->add(MailjetNewsletterTableMap::MAILJET_ID, $this->mailjet_id);
if ($this->isColumnModified(MailjetNewsletterTableMap::EMAIL)) $criteria->add(MailjetNewsletterTableMap::EMAIL, $this->email);
if ($this->isColumnModified(MailjetNewsletterTableMap::RELATION_ID)) $criteria->add(MailjetNewsletterTableMap::RELATION_ID, $this->relation_id);

Expand Down Expand Up @@ -965,11 +1032,12 @@ public function isPrimaryKeyNull()
*/
public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
{
$copyObj->setId($this->getId());
$copyObj->setMailjetId($this->getMailjetId());
$copyObj->setEmail($this->getEmail());
$copyObj->setRelationId($this->getRelationId());
if ($makeNew) {
$copyObj->setNew(true);
$copyObj->setId(NULL); // this is a auto-increment column, so set to default value
}
}

Expand Down Expand Up @@ -1001,6 +1069,7 @@ public function copy($deepCopy = false)
public function clear()
{
$this->id = null;
$this->mailjet_id = null;
$this->email = null;
$this->relation_id = null;
$this->alreadyInSave = false;
Expand Down
35 changes: 34 additions & 1 deletion Model/Base/MailjetNewsletterQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
*
*
* @method ChildMailjetNewsletterQuery orderById($order = Criteria::ASC) Order by the id column
* @method ChildMailjetNewsletterQuery orderByMailjetId($order = Criteria::ASC) Order by the mailjet_id column
* @method ChildMailjetNewsletterQuery orderByEmail($order = Criteria::ASC) Order by the email column
* @method ChildMailjetNewsletterQuery orderByRelationId($order = Criteria::ASC) Order by the relation_id column
*
* @method ChildMailjetNewsletterQuery groupById() Group by the id column
* @method ChildMailjetNewsletterQuery groupByMailjetId() Group by the mailjet_id column
* @method ChildMailjetNewsletterQuery groupByEmail() Group by the email column
* @method ChildMailjetNewsletterQuery groupByRelationId() Group by the relation_id column
*
Expand All @@ -34,10 +36,12 @@
* @method ChildMailjetNewsletter findOneOrCreate(ConnectionInterface $con = null) Return the first ChildMailjetNewsletter matching the query, or a new ChildMailjetNewsletter object populated from the query conditions when no match is found
*
* @method ChildMailjetNewsletter findOneById(int $id) Return the first ChildMailjetNewsletter filtered by the id column
* @method ChildMailjetNewsletter findOneByMailjetId(string $mailjet_id) Return the first ChildMailjetNewsletter filtered by the mailjet_id column
* @method ChildMailjetNewsletter findOneByEmail(string $email) Return the first ChildMailjetNewsletter filtered by the email column
* @method ChildMailjetNewsletter findOneByRelationId(int $relation_id) Return the first ChildMailjetNewsletter filtered by the relation_id column
*
* @method array findById(int $id) Return ChildMailjetNewsletter objects filtered by the id column
* @method array findByMailjetId(string $mailjet_id) Return ChildMailjetNewsletter objects filtered by the mailjet_id column
* @method array findByEmail(string $email) Return ChildMailjetNewsletter objects filtered by the email column
* @method array findByRelationId(int $relation_id) Return ChildMailjetNewsletter objects filtered by the relation_id column
*
Expand Down Expand Up @@ -128,7 +132,7 @@ public function findPk($key, $con = null)
*/
protected function findPkSimple($key, $con)
{
$sql = 'SELECT ID, EMAIL, RELATION_ID FROM mailjet_newsletter WHERE ID = :p0';
$sql = 'SELECT ID, MAILJET_ID, EMAIL, RELATION_ID FROM mailjet_newsletter WHERE ID = :p0';
try {
$stmt = $con->prepare($sql);
$stmt->bindValue(':p0', $key, PDO::PARAM_INT);
Expand Down Expand Up @@ -258,6 +262,35 @@ public function filterById($id = null, $comparison = null)
return $this->addUsingAlias(MailjetNewsletterTableMap::ID, $id, $comparison);
}

/**
* Filter the query on the mailjet_id column
*
* Example usage:
* <code>
* $query->filterByMailjetId('fooValue'); // WHERE mailjet_id = 'fooValue'
* $query->filterByMailjetId('%fooValue%'); // WHERE mailjet_id LIKE '%fooValue%'
* </code>
*
* @param string $mailjetId The value to use as filter.
* Accepts wildcards (* and % trigger a LIKE)
* @param string $comparison Operator to use for the column comparison, defaults to Criteria::EQUAL
*
* @return ChildMailjetNewsletterQuery The current query, for fluid interface
*/
public function filterByMailjetId($mailjetId = null, $comparison = null)
{
if (null === $comparison) {
if (is_array($mailjetId)) {
$comparison = Criteria::IN;
} elseif (preg_match('/[\%\*]/', $mailjetId)) {
$mailjetId = str_replace('*', '%', $mailjetId);
$comparison = Criteria::LIKE;
}
}

return $this->addUsingAlias(MailjetNewsletterTableMap::MAILJET_ID, $mailjetId, $comparison);
}

/**
* Filter the query on the email column
*
Expand Down
Loading

0 comments on commit 07cee38

Please sign in to comment.