Zend Framework 2中的Hydrator问题

时间:2016-04-27 14:59:33

标签: php zend-framework2

引擎收录

HERE is my pastebin!

我在AggregateHydrator添加了三个自定义水合器:

$hydrator = new AggregateHydrator();
$hydrator->add(new ClassMethods());
$hydrator->add(new \Application\Hydrator\OrderedUserHydrator());
$hydrator->add(new \Application\Hydrator\ArrivedUserHydrator());
$hydrator->add(new \Application\Hydrator\FittedUserHydrator());

我遇到的问题是 OrderedUserHydrator 似乎仅在 ArrivedUserHydrator 时才有效。实际上, OrderedUserHydrator 应始终保持水分。但事实并非如此:

/var/www/zf-skeleton/module/Application/src/Application/Mapper/ZendDbSqlMapper.php:585:
object(Application\Model\Parts)[786]
  protected 'id' => string '16' (length=2)
  protected 'vehicle_id' => string '3' (length=1)
  protected 'status' => string '0' (length=1)
  protected 'part_name' => string 'Test part' (length=9)
  protected 'ordered_from' => string 'A SUPPLIER' (length=10)
  protected 'ordered_user_id' => string '1' (length=1)
  protected 'arrived_user_id' => null
  protected 'fitted_user_id' => string '1' (length=1)
  protected 'ordered_date' => string '2016-04-06 00:00:00' (length=19)
  protected 'arrived_date' => null
  protected 'fitted_date' => string '2016-04-27 15:03:24' (length=19)
  protected 'ordered_user' =>
    object(Application\Model\User)[803]
      protected 'user_id' => null
      protected 'username' => null
      protected 'email' => null <!-- THIS SHOULD BE SET
      protected 'display_name' => null <!-- THIS SHOULD BE SET
  protected 'arrived_user' =>
    object(Application\Model\User)[804]
      protected 'user_id' => null
      protected 'username' => null
      protected 'email' => null
      protected 'display_name' => null
  protected 'fitted_user' =>
    object(Application\Model\User)[805]
      protected 'user_id' => null
      protected 'username' => null
      protected 'email' => string 'alex@home.com' (length=13)
      protected 'display_name' => string 'Alex Foobar' (length=13)

只要 ArrivedUserHydrator 能够保湿, OrderedUserHydrator 就会起作用:

/var/www/zf-skeleton/module/Application/src/Application/Mapper/ZendDbSqlMapper.php:585:
object(Application\Model\Parts)[786]
  protected 'id' => string '16' (length=2)
  protected 'vehicle_id' => string '3' (length=1)
  protected 'status' => string '0' (length=1)
  protected 'part_name' => string 'Test part' (length=9)
  protected 'ordered_from' => string 'A SUPPLIER' (length=10)
  protected 'ordered_user_id' => string '1' (length=1)
  protected 'arrived_user_id' => string '1' (length=1)
  protected 'fitted_user_id' => string '1' (length=1)
  protected 'ordered_date' => string '2016-04-06 00:00:00' (length=19)
  protected 'arrived_date' => string '2016-04-27 15:01:29' (length=19)
  protected 'fitted_date' => string '2016-04-27 15:01:29' (length=19)
  protected 'ordered_user' =>
    object(Application\Model\User)[803]
      protected 'user_id' => null
      protected 'username' => null
      protected 'email' => string 'alex@home.com' (length=13)
      protected 'display_name' => string 'Alex Foobar' (length=13)
  protected 'arrived_user' =>
    object(Application\Model\User)[804]
      protected 'user_id' => null
      protected 'username' => null
      protected 'email' => string 'alex@home.com' (length=13)
      protected 'display_name' => string 'Alex Foobar' (length=13)
  protected 'fitted_user' =>
    object(Application\Model\User)[805]
      protected 'user_id' => null
      protected 'username' => null
      protected 'email' => string 'alex@home.com' (length=13)
      protected 'display_name' => string 'Alex Foobar' (length=13)

非常感谢任何有关此事的帮助。我一直在撕扯我的头发。

我的保湿剂如下:

/home/user/development/zf-skeleton/module/Application/src/Application/Hydrator/OrderedUserHydrator.php:

use Zend\Stdlib\Hydrator\ClassMethods;
use Application\Model\User;

class OrderedUserHydrator extends ClassMethods
{
    public function hydrate(array $data, $object)
    {
        $userData = [];

        foreach ($data as $key => $value) {

            $userData[substr($key, 12)] = $value;

            if ('ordereduser_' === substr($key, 0, 12)) {
                $userData[substr($key, 12)] = $value;
            }

        }

        $ordered_user = parent::hydrate($userData, new User());

        $object->setOrderedUser($ordered_user);

        return $object;
    }
}

/home/user/development/zf-skeleton/module/Application/src/Application/Hydrator/ArrivedUserHydrator.php:

use Zend\Stdlib\Hydrator\ClassMethods;
use Application\Model\User;

class ArrivedUserHydrator extends ClassMethods
{
    public function hydrate(array $data, $object)
    {
        $userData = [];

        foreach ($data as $key => $value) {

            $userData[substr($key, 12)] = $value;

            if ('arriveduser_' === substr($key, 0, 12)) {
                $userData[substr($key, 12)] = $value;
            }


        }

        $arrived_user = parent::hydrate($userData, new User());

        $object->setArrivedUser($arrived_user);

        return $object;
    }
}

/home/user/development/zf-skeleton/module/Application/src/Application/Hydrator/FittedUserHydrator.php:

use Zend\Stdlib\Hydrator\ClassMethods;
use Application\Model\User;

class FittedUserHydrator extends ClassMethods
{
    public function hydrate(array $data, $object)
    {
        $userData = [];

        foreach ($data as $key => $value) {

            $userData[substr($key, 11)] = $value;

            if ('fitteduser_' === substr($key, 0, 11)) {
                $userData[substr($key, 11)] = $value;
            }

        }

        $fitted_user = parent::hydrate($userData, new User());

        $object->setFittedUser($fitted_user);

        return $object;
    }
}

1 个答案:

答案 0 :(得分:0)

你的所有保湿器都有一个错误。从所有加水器中取出这条线,应该可以工作:

$userData[substr($key, 11)] = $value;