PHP code example of troytft / data-mapper-bundle

1. Go to this page and download the library: Download troytft/data-mapper-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

troytft / data-mapper-bundle example snippets

 php
$bundles = array(
    // ...
    new Troytft\DataMapperBundle\DataMapperBundle(),
);
 php

namespace Common\Model;

use Common\Constraint as AppAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Troytft\DataMapperBundle\Annotation\DataMapper;

class PostsFilter
{
    /**
     * @DataMapper(type="string")
     */
    protected $query;

    /**
     * @DataMapper(type="entity", options={"class": "CommonBundle:City"})
     * @Assert\NotBlank
     */
    protected $city;

    /**
     * @return mixed
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * @param mixed $value
     */
    public function setCity($value)
    {
        $this->city = $value;

        return $this;
    }

    /**
     * @return string
     */
    public function getQuery()
    {
        return $this->query;
    }

    /**
     * @param string $value
     */
    public function setQuery($value)
    {
        $this->query = $value;
        
        return $this;
    }
}
 php
    public function handleRequest($model, $clearMissing = true, $groups = ['Default'], $validationGroups = ['Default'])
    {
        /** @var Request $request */
        $request = $this->get('request');
        $data = $request->getRealMethod() == 'GET' ? $request->query->all() : $request->request->all();

        /** @var DataMapperManager $manager */
        $manager = $this->get('data_mapper.manager');

        return $manager
            ->setGroups($groups)
            ->setValidationGroups($validationGroups)
            ->setIsClearMissing($clearMissing)
            ->handle($model, $data);
    }