fix segments being reset on Subscriber::createOrUpdate()

This commit is contained in:
Jonathan Labreuille
2016-02-16 15:03:08 +01:00
parent 316d5ab183
commit 6a2e18a0e1
3 changed files with 17 additions and 8 deletions

View File

@ -56,6 +56,10 @@ class Initializer {
\ORM::configure('username', Env::$db_username); \ORM::configure('username', Env::$db_username);
\ORM::configure('password', Env::$db_password); \ORM::configure('password', Env::$db_password);
\ORM::configure('logging', WP_DEBUG); \ORM::configure('logging', WP_DEBUG);
\ORM::configure('logger', function($query, $time) {
error_log("\n(Time: ".sprintf('%05f', $time).")".$query."\n");
});
\ORM::configure('driver_options', array( \ORM::configure('driver_options', array(
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET TIME_ZONE = "+00:00"' \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET TIME_ZONE = "+00:00"'

View File

@ -33,8 +33,12 @@ class Subscriber extends Model {
} }
function addToSegments(array $segment_ids = array()) { function addToSegments(array $segment_ids = array()) {
// delete all relations to segments $wp_users_segment = Segment::getWPUsers();
SubscriberSegment::where('subscriber_id', $this->id)->deleteMany();
// delete all relations to segments except WP users
SubscriberSegment::where('subscriber_id', $this->id)
->whereNotEqual('segment_id', $wp_users_segment->id)
->deleteMany();
if(!empty($segment_ids)) { if(!empty($segment_ids)) {
$segments = Segment::whereIn('id', $segment_ids)->findMany(); $segments = Segment::whereIn('id', $segment_ids)->findMany();
@ -252,9 +256,8 @@ class Subscriber extends Model {
} }
// segments // segments
$segment_ids = array(); $segment_ids = false;
if(array_key_exists('segments', $data)) {
if(isset($data['segments'])) {
$segment_ids = (array)$data['segments']; $segment_ids = (array)$data['segments'];
unset($data['segments']); unset($data['segments']);
} }
@ -282,7 +285,9 @@ class Subscriber extends Model {
$subscriber->setCustomField($custom_field_id, $value); $subscriber->setCustomField($custom_field_id, $value);
} }
} }
$subscriber->addToSegments($segment_ids); if($segment_ids !== false) {
$subscriber->addToSegments($segment_ids);
}
} }
return $subscriber; return $subscriber;
} }

View File

@ -47,9 +47,9 @@ class WP {
} }
$subscriber = Subscriber::createOrUpdate($data); $subscriber = Subscriber::createOrUpdate($data);
if($subscriber !== false && $subscriber->id()) { if($subscriber->getErrors() === false && $subscriber->id > 0) {
if($segment !== false) { if($segment !== false) {
$segment->addSubscriber($subscriber->id()); $segment->addSubscriber($subscriber->id);
} }
} }
break; break;