Symfony2 Validation against multiple fields - validation

I have a contestEntry entity.
It has a number of properties (player Positions) all linking to a fantasyPlayer entity.
On submission of a form i need to check that the different player positions are unique (ie a player has not been put in 2 positions)
Is it possible to use validations for this, as far i can see i can only apply a validation to a property, is there a way to apply validations across the whole entity?
ContestEntry:
<?php
namespace FantasyPro\GameBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
use FantasyPro\DataBundle\Entity\DailyFantasyPlayer;
use FantasyPro\UserBundle\Entity\User;
/**
* #ORM\Entity(repositoryClass="FantasyPro\GameBundle\Entity\ContestEntryRepository")
* #ORM\Table(name="fp_contestEntry")
*/
class ContestEntry
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="id")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="boolean", nullable=true, name="locked")
*/
private $locked;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\UserBundle\Entity\User", inversedBy="contestEntries")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="qb", referencedColumnName="id")
*/
private $qb;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="wr1", referencedColumnName="id")
*/
private $wr1;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="wr2", referencedColumnName="id")
*/
private $wr2;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="rb1", referencedColumnName="id")
*/
private $rb1;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="rb2", referencedColumnName="id")
*/
private $rb2;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="te", referencedColumnName="id")
*/
private $te;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="k", referencedColumnName="id")
*/
private $k;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="flex", referencedColumnName="id")
*/
private $flex;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\DataBundle\Entity\DailyFantasyPlayer")
* #ORM\JoinColumn(name="def", referencedColumnName="id")
*/
private $def;
/**
* #ORM\ManyToOne(targetEntity="FantasyPro\GameBundle\Entity\Contest", inversedBy="contestEntries")
* #ORM\JoinColumn(name="contest", referencedColumnName="id")
*/
private $contest;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locked
*
* #param boolean $locked
*
* #return ContestEntry
*/
public function setLocked($locked)
{
$this->locked = $locked;
return $this;
}
/**
* Get locked
*
* #return boolean
*/
public function getLocked()
{
return $this->locked;
}
/**
* Set user
*
* #param \FantasyPro\UserBundle\Entity\User $user
*
* #return ContestEntry
*/
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return \FantasyPro\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set qb
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $qb
*
* #return ContestEntry
*/
public function setQb(DailyFantasyPlayer $qb = null)
{
$this->qb = $qb;
return $this;
}
/**
* Get qb
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getQb()
{
return $this->qb;
}
/**
* Set wr1
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $wr1
*
* #return ContestEntry
*/
public function setWr1(DailyFantasyPlayer $wr1 = null)
{
$this->wr1 = $wr1;
return $this;
}
/**
* Get wr1
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getWr1()
{
return $this->wr1;
}
/**
* Set wr2
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $wr2
*
* #return ContestEntry
*/
public function setWr2(DailyFantasyPlayer $wr2 = null)
{
$this->wr2 = $wr2;
return $this;
}
/**
* Get wr2
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getWr2()
{
return $this->wr2;
}
/**
* Set rb1
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $rb1
*
* #return ContestEntry
*/
public function setRb1(DailyFantasyPlayer $rb1 = null)
{
$this->rb1 = $rb1;
return $this;
}
/**
* Get rb1
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getRb1()
{
return $this->rb1;
}
/**
* Set rb2
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $rb2
*
* #return ContestEntry
*/
public function setRb2(DailyFantasyPlayer $rb2 = null)
{
$this->rb2 = $rb2;
return $this;
}
/**
* Get rb2
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getRb2()
{
return $this->rb2;
}
/**
* Set te
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $te
*
* #return ContestEntry
*/
public function setTe(DailyFantasyPlayer $te = null)
{
$this->te = $te;
return $this;
}
/**
* Get te
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getTe()
{
return $this->te;
}
/**
* Set k
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $k
*
* #return ContestEntry
*/
public function setK(DailyFantasyPlayer $k = null)
{
$this->k = $k;
return $this;
}
/**
* Get k
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getK()
{
return $this->k;
}
/**
* Set flex
*
* #param \FantasyPro\DataBundle\Entity\DailyFantasyPlayer $flex
*
* #return ContestEntry
*/
public function setFlex(DailyFantasyPlayer $flex = null)
{
$this->flex = $flex;
return $this;
}
/**
* Get flex
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getFlex()
{
return $this->flex;
}
/**
* Set def
*
* #param DailyFantasyPlayer $def
*
* #return ContestEntry
*/
public function setDef(DailyFantasyPlayer $def = null)
{
$this->def = $def;
return $this;
}
/**
* Get def
*
* #return \FantasyPro\DataBundle\Entity\DailyFantasyPlayer
*/
public function getDef()
{
return $this->def;
}
/**
* Set contest
*
* #param \FantasyPro\GameBundle\Entity\Contest $contest
*
* #return ContestEntry
*/
public function setContest(Contest $contest = null)
{
$this->contest = $contest;
return $this;
}
/**
* Get contest
*
* #return \FantasyPro\GameBundle\Entity\Contest
*/
public function getContest()
{
return $this->contest;
}
}
I have a contestHelper class containing various methods, one of them is to check that the selected fantasyPlayers are unique
/**
* Checks to see that the players selected for the squad are unique
* i.e no player/team has been selected twice
*
* #param ContestEntry $contestEntry
*
* #return bool
*/
public function squadPlayersAreUnique( ContestEntry $contestEntry )
{
//checks to see there are no duplicate players in the squad
$playerArray = array(
$contestEntry->getWr1()->getPlayer()->getId(),
$contestEntry->getWr2()->getPlayer()->getId(),
$contestEntry->getRb1()->getPlayer()->getId(),
$contestEntry->getRb2()->getPlayer()->getId(),
$contestEntry->getTe()->getPlayer()->getId(),
$contestEntry->getK()->getPlayer()->getId(),
$contestEntry->getFlex()->getPlayer()->getId(),
$contestEntry->getDef()->getTeam()->getPlayerID()
);
return count( $playerArray ) == count( array_unique( $playerArray ) );
}
My controller is very wrong as i have a call to squadPlayersAreUnique within my validation controller, and no way to redirect properly back to the page.
Is there a way to add this validation to the entire entity itself?
heres my controller in its current state:
/**
* Shows a form for Updating a contestEntry.
*
* #Route("/manageentry/{id}", name="update_contestentry")
* #Template("GameBundle:ContestEntry:update.html.twig")
* #param ContestEntry $contestEntry
* #ParamConverter("contestEntry", class="GameBundle:ContestEntry", options={"id" = "id"})
* #return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function updateEntryAction( ContestEntry $contestEntry = null )
{
$session = new Session();
$session->start();
// add flash messages
if ( empty( $contestEntry)) {
//$session->getFlashBag()->add('error', 'Sorry, We could not find your contest entry!');
throw $this->createNotFoundException(
'Sorry, We could not find your contest entry!'
);
}
$request = Request::createFromGlobals();
$contestHelper = $this->get( 'contest_helper' );
$contest = $contestEntry->getContest();
$availablePlayers = $contestHelper->getPlayersforContestResult( $contest );
$form = $this->createForm( 'contestEntry', $contestEntry );
$form->handleRequest( $request );
if ($form->isValid()) {
//check there are no duplicate players
if( !$contestHelper->squadPlayersAreUnique( $contestEntry ) ){
//throw new HttpException(500, 'Duplicate players found in your squad!');
$session->getFlashBag()->add('error', 'Duplicate players found in your squad, Please check your entry and try again!');
}
if( !$contestHelper->entryNotOverBudget( $contestEntry) ){
// throw new HttpException(500, 'Not Enough funds to draft selected players!');
$session->getFlashBag()->add('error', 'Not Enough funds to draft selected players, Please check your entry and try again!');
};
$em = $this->getDoctrine()->getManager();
$em->persist( $contestEntry );
$em->flush();
$this->addFlash(
'success',
'Contest Entry Updated!!'
);
return $this->redirect( $this->generateUrl( 'list_contestentries' ) );
}
return array(
'contestEntry' => $contestEntry,
'contestForm' => $form->createView(),
'contest' => $contest,
'playerList' => $availablePlayers
);
}

you can add the callback validator on entity level:
http://symfony.com/doc/current/reference/constraints/Callback.html

Related

Pusher\\ApiErrorException(code: 0) error in Chat system

After Message save broadcasting occurs this issue:
local.ERROR: {"userId":3,"exception":"[object] (Pusher\ApiErrorException(code: 0): at C:\xampp\htdocs\fm\script\vendor\pusher\pusher-php-server\src\Pusher.php:533)
[stacktrace]
#0 C:\xampp\htdocs\fm\script\vendor
nahid\talk\src\Live\Webcast.php(51): Pusher\Pusher->trigger(Array, 'talk-send-messa...', Array)
nahid\talk\src\Live\Broadcast.php(85): Nahid\Talk\Live\Broadcast->dispatch(Object(Nahid\Talk\Live\Webcast))
#22 C:\xampp\htdocs\fm\script\vendor
nahid\talk\src\Talk.php(106): Nahid\Talk\Live\Broadcast->transmission(Object(Nahid\Talk\Messages\Message))
#23 C:\xampp\htdocs\fm\script\vendor
nahid\talk\src\Talk.php(268): Nahid\Talk\Talk->makeMessage(2, Object(Nahid\Talk\Messages\Message))
Pages:
Webcast.php
class Webcast implements ShouldQueue
{
use InteractsWithQueue, Queueable, SerializesModels;
/*
* Message Model Instance
*
* #var object
* */
protected $message;
/*
* Broadcast class instance
*
* #var object
* */
protected $broadcast;
/**
* Set message collections to the properties.
*/
public function __construct($message)
{
$this->message = $message;
}
/*
* Execute the job and broadcast to the pusher channels
*
* #param \Nahid\Talk\Live\Broadcast $broadcast
* #return void
*/
public function handle(Broadcast $broadcast)
{
$this->broadcast = $broadcast;
$senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
$toUser = ($this->message['sender'][ $senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
$channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
$channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
// try {
$this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
// } catch (ApiErrorException $e) {
// throw new BroadcastException(
// sprintf('Pusher error: %s.', $e->getMessage())
// );
// }
}
}
Broadcast.php
class Broadcast
{
use DispatchesJobs;
/*
* Constant for talk config prefix
*
* #const string
* */
const CONFIG_PATH = 'talk';
/*
* Set all configs from talk configurations
*
* #var array
* */
protected $config;
/*
* Pusher instance
*
* #var object
* */
public $pusher;
/**
* Connect pusher and get all credentials from config.
*
* #param \Illuminate\Contracts\Config\Repository $config
*/
public function __construct(Repository $config)
{
$this->config = $config;
$this->pusher = $this->connectPusher();
}
/**
* Make pusher connection.
*
* #param array $options
*
* #return object | bool
*/
protected function connectPusher($options = [])
{
if ($this->getConfig('broadcast.enable')) {
$appId = $this->getConfig('broadcast.pusher.app_id');
$appKey = $this->getConfig('broadcast.pusher.app_key');
$appSecret = $this->getConfig('broadcast.pusher.app_secret');
$appOptions = $this->getConfig('broadcast.pusher.options');
$newOptions = array_merge($appOptions, $options);
$pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
return $pusher;
}
return false;
}
/**
* Dispatch the job to the queue.
*
* #param \Nahid\Talk\Messages\Message $message
*/
public function transmission(Message $message)
{
if (!$this->pusher) {
return false;
}
$sender = $message->sender->toArray();
$messageArray = $message->toArray();
$messageArray['sender'] = $sender;
$this->dispatch(new Webcast($messageArray));
}
/**
* get specific config from talk configurations.
*
* #param string
*
* #return string|array|int
*/
public function getConfig($name)
{
return $this->config->get(self::CONFIG_PATH.'.'.$name);
}
}

Product images are serving from different path in Frontend and Backend

Frontend Product images are serving from this path1 :
media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/i/m/img24.jpg
Backend Product images are serving from this path2
media/catalog/product/i/m/img24.jpg
why same images stored in different path.
I think Backend images are serving from proper path, I want frontend images also should serve from same path.
so we followed link & added following code in below file, but after that our site did't load any images.:
app/code/core/Mage/Catalog/Helper/Image.php
echo Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getSmallImage());
full code :
<?php
class Mage_Catalog_Helper_Image extends Mage_Core_Helper_Abstract
{
/**
* Current model
*
* #var Mage_Catalog_Model_Product_Image
*/
protected $_model;
/**
* Scheduled for resize image
*
* #var bool
*/
protected $_scheduleResize = false;
/**
* Scheduled for rotate image
*
* #var bool
*/
protected $_scheduleRotate = false;
/**
* Angle
*
* #var int
*/
protected $_angle;
/**
* Watermark file name
*
* #var string
*/
protected $_watermark;
/**
* Watermark Position
*
* #var string
*/
protected $_watermarkPosition;
/**
* Watermark Size
*
* #var string
*/
protected $_watermarkSize;
/**
* Watermark Image opacity
*
* #var int
*/
protected $_watermarkImageOpacity;
/**
* Current Product
*
* #var Mage_Catalog_Model_Product
*/
protected $_product;
/**
* Image File
*
* #var string
*/
protected $_imageFile;
/**
* Image Placeholder
*
* #var string
*/
protected $_placeholder;
/**
* Reset all previous data
*
* #return Mage_Catalog_Helper_Image
*/
protected function _reset()
{
$this->_model = null;
$this->_scheduleResize = false;
$this->_scheduleRotate = false;
$this->_angle = null;
$this->_watermark = null;
$this->_watermarkPosition = null;
$this->_watermarkSize = null;
$this->_watermarkImageOpacity = null;
$this->_product = null;
$this->_imageFile = null;
return $this;
}
/**
* Initialize Helper to work with Image
*
* #param Mage_Catalog_Model_Product $product
* #param string $attributeName
* #param mixed $imageFile
* #return Mage_Catalog_Helper_Image
*/
public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
{
$this->_reset();
$this->_setModel(Mage::getModel('catalog/product_image'));
$this->_getModel()->setDestinationSubdir($attributeName);
$this->setProduct($product);
$this->setWatermark(
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_image")
);
$this->setWatermarkImageOpacity(
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_imageOpacity")
);
$this->setWatermarkPosition(
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_position")
);
$this->setWatermarkSize(
Mage::getStoreConfig("design/watermark/{$this->_getModel()->getDestinationSubdir()}_size")
);
if ($imageFile) {
$this->setImageFile($imageFile);
} else {
// add for work original size
$this->_getModel()->setBaseFile($this->getProduct()->getData($this->_getModel()->getDestinationSubdir()));
}
return $this;
}
/**
* Schedule resize of the image
* $width *or* $height can be null - in this case, lacking dimension will be calculated.
*
* #see Mage_Catalog_Model_Product_Image
* #param int $width
* #param int $height
* #return Mage_Catalog_Helper_Image
*/
public function resize($width, $height = null)
{
$this->_getModel()->setWidth($width)->setHeight($height);
$this->_scheduleResize = true;
return $this;
}
/**
* Set image quality, values in percentage from 0 to 100
*
* #param int $quality
* #return Mage_Catalog_Helper_Image
*/
public function setQuality($quality)
{
$this->_getModel()->setQuality($quality);
return $this;
}
/**
* Guarantee, that image picture width/height will not be distorted.
* Applicable before calling resize()
* It is true by default.
*
* #see Mage_Catalog_Model_Product_Image
* #param bool $flag
* #return Mage_Catalog_Helper_Image
*/
public function keepAspectRatio($flag)
{
$this->_getModel()->setKeepAspectRatio($flag);
return $this;
}
/**
* Guarantee, that image will have dimensions, set in $width/$height
* Applicable before calling resize()
* Not applicable, if keepAspectRatio(false)
*
* $position - TODO, not used for now - picture position inside the frame.
*
* #see Mage_Catalog_Model_Product_Image
* #param bool $flag
* #param array $position
* #return Mage_Catalog_Helper_Image
*/
public function keepFrame($flag, $position = array('center', 'middle'))
{
$this->_getModel()->setKeepFrame($flag);
return $this;
}
/**
* Guarantee, that image will not lose transparency if any.
* Applicable before calling resize()
* It is true by default.
*
* $alphaOpacity - TODO, not used for now
*
* #see Mage_Catalog_Model_Product_Image
* #param bool $flag
* #param int $alphaOpacity
* #return Mage_Catalog_Helper_Image
*/
public function keepTransparency($flag, $alphaOpacity = null)
{
$this->_getModel()->setKeepTransparency($flag);
return $this;
}
/**
* Guarantee, that image picture will not be bigger, than it was.
* Applicable before calling resize()
* It is false by default
*
* #param bool $flag
* #return Mage_Catalog_Helper_Image
*/
public function constrainOnly($flag)
{
$this->_getModel()->setConstrainOnly($flag);
return $this;
}
/**
* Set color to fill image frame with.
* Applicable before calling resize()
* The keepTransparency(true) overrides this (if image has transparent color)
* It is white by default.
*
* #see Mage_Catalog_Model_Product_Image
* #param array $colorRGB
* #return Mage_Catalog_Helper_Image
*/
public function backgroundColor($colorRGB)
{
// assume that 3 params were given instead of array
if (!is_array($colorRGB)) {
$colorRGB = func_get_args();
}
$this->_getModel()->setBackgroundColor($colorRGB);
return $this;
}
/**
* Rotate image into specified angle
*
* #param int $angle
* #return Mage_Catalog_Helper_Image
*/
public function rotate($angle)
{
$this->setAngle($angle);
$this->_getModel()->setAngle($angle);
$this->_scheduleRotate = true;
return $this;
}
/**
* Add watermark to image
* size param in format 100x200
*
* #param string $fileName
* #param string $position
* #param string $size
* #param int $imageOpacity
* #return Mage_Catalog_Helper_Image
*/
public function watermark($fileName, $position, $size=null, $imageOpacity=null)
{
$this->setWatermark($fileName)
->setWatermarkPosition($position)
->setWatermarkSize($size)
->setWatermarkImageOpacity($imageOpacity);
return $this;
}
/**
* Set placeholder
*
* #param string $fileName
* #return void
*/
public function placeholder($fileName)
{
$this->_placeholder = $fileName;
}
/**
* Get Placeholder
*
* #return string
*/
public function getPlaceholder()
{
if (!$this->_placeholder) {
$attr = $this->_getModel()->getDestinationSubdir();
$this->_placeholder = 'images/catalog/product/placeholder/'.$attr.'.jpg';
}
return $this->_placeholder;
}
/**
* Return Image URL
*
* #return string
*/
public function __toString()
{
try {
$model = $this->_getModel();
if ($this->getImageFile()) {
$model->setBaseFile($this->getImageFile());
} else {
$model->setBaseFile($this->getProduct()->getData($model->getDestinationSubdir()));
}
if ($model->isCached()) {
return $model->getUrl();
} else {
if ($this->_scheduleRotate) {
$model->rotate($this->getAngle());
}
if ($this->_scheduleResize) {
$model->resize();
}
if ($this->getWatermark()) {
$model->setWatermark($this->getWatermark());
}
$url = $model->saveFile()->getUrl();
}
} catch (Exception $e) {
$url = Mage::getDesign()->getSkinUrl($this->getPlaceholder());
}
return $url;
}
/**
* Set current Image model
*
* #param Mage_Catalog_Model_Product_Image $model
* #return Mage_Catalog_Helper_Image
*/
protected function _setModel($model)
{
$this->_model = $model;
return $this;
}
/**
* Get current Image model
*
* #return Mage_Catalog_Model_Product_Image
*/
protected function _getModel()
{
return $this->_model;
}
/**
* Set Rotation Angle
*
* #param int $angle
* #return Mage_Catalog_Helper_Image
*/
protected function setAngle($angle)
{
$this->_angle = $angle;
return $this;
}
/**
* Get Rotation Angle
*
* #return int
*/
protected function getAngle()
{
return $this->_angle;
}
/**
* Set watermark file name
*
* #param string $watermark
* #return Mage_Catalog_Helper_Image
*/
protected function setWatermark($watermark)
{
$this->_watermark = $watermark;
$this->_getModel()->setWatermarkFile($watermark);
return $this;
}
/**
* Get watermark file name
*
* #return string
*/
protected function getWatermark()
{
return $this->_watermark;
}
/**
* Set watermark position
*
* #param string $position
* #return Mage_Catalog_Helper_Image
*/
protected function setWatermarkPosition($position)
{
$this->_watermarkPosition = $position;
$this->_getModel()->setWatermarkPosition($position);
return $this;
}
/**
* Get watermark position
*
* #return string
*/
protected function getWatermarkPosition()
{
return $this->_watermarkPosition;
}
/**
* Set watermark size
* param size in format 100x200
*
* #param string $size
* #return Mage_Catalog_Helper_Image
*/
public function setWatermarkSize($size)
{
$this->_watermarkSize = $size;
$this->_getModel()->setWatermarkSize($this->parseSize($size));
return $this;
}
/**
* Get watermark size
*
* #return string
*/
protected function getWatermarkSize()
{
return $this->_watermarkSize;
}
/**
* Set watermark image opacity
*
* #param int $imageOpacity
* #return Mage_Catalog_Helper_Image
*/
public function setWatermarkImageOpacity($imageOpacity)
{
$this->_watermarkImageOpacity = $imageOpacity;
$this->_getModel()->setWatermarkImageOpacity($imageOpacity);
return $this;
}
/**
* Get watermark image opacity
*
* #return int
*/
protected function getWatermarkImageOpacity()
{
if ($this->_watermarkImageOpacity) {
return $this->_watermarkImageOpacity;
}
return $this->_getModel()->getWatermarkImageOpacity();
}
/**
* Set current Product
*
* #param Mage_Catalog_Model_Product $product
* #return Mage_Catalog_Helper_Image
*/
protected function setProduct($product)
{
$this->_product = $product;
return $this;
}
/**
* Get current Product
*
* #return Mage_Catalog_Model_Product
*/
protected function getProduct()
{
return $this->_product;
}
/**
* Set Image file
*
* #param string $file
* #return Mage_Catalog_Helper_Image
*/
protected function setImageFile($file)
{
$this->_imageFile = $file;
return $this;
}
/**
* Get Image file
*
* #return string
*/
protected function getImageFile()
{
return $this->_imageFile;
}
/**
* Retrieve size from string
*
* #param string $string
* #return array|bool
*/
protected function parseSize($string)
{
$size = explode('x', strtolower($string));
if (sizeof($size) == 2) {
return array(
'width' => ($size[0] > 0) ? $size[0] : null,
'heigth' => ($size[1] > 0) ? $size[1] : null,
);
}
return false;
}
/**
* Retrieve original image width
*
* #return int|null
*/
public function getOriginalWidth()
{
return $this->_getModel()->getImageProcessor()->getOriginalWidth();
}
/**
* Retrieve original image height
*
* #deprecated
* #return int|null
*/
public function getOriginalHeigh()
{
return $this->getOriginalHeight();
}
/**
* Retrieve original image height
*
* #return int|null
*/
public function getOriginalHeight()
{
return $this->_getModel()->getImageProcessor()->getOriginalHeight();
}
/**
* Retrieve Original image size as array
* 0 - width, 1 - height
*
* #return array
*/
public function getOriginalSizeArray()
{
return array(
$this->getOriginalWidth(),
$this->getOriginalHeight()
);
}
/**
* Check - is this file an image
*
* #param string $filePath
* #return bool
* #throws Mage_Core_Exception
*/
public function validateUploadFile($filePath) {
if (!getimagesize($filePath)) {
Mage::throwException($this->__('Disallowed file type.'));
}
$_processor = new Varien_Image($filePath);
return $_processor->getMimeType() !== null;
}
echo Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getSmallImage());
}
In frontend all product image come from cache due to increase speed.
In backend Magento not consider speed so it come from real image path.
You can not written any code out side of method because Magento is fully object oriented.
You can use below code for fetch image full url.
echo $imageUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . 'catalog/product' . $_product->getImage();

Fatal error: Call to a member function setFinalPrice() on a non-object in /app/code/core/Mage/Sales/Model/Quote/Item/Abstract.php on line 89

I had installed an custom extension. Its running successfully and had all files at its place. But I am getting this error on my listing page
Fatal error: Call to a member function setFinalPrice() on a non-object in /var/zpanel/hostdata/zadmin/public_html/unisport_com/app/code/core/Mage/Sales/Model/Quote/Item/Abstract.php on line 89.
I google this error but did'nt found any proper solution. One solution is to copy the original Abstract.php file may be because extension had overwrite this file. But I checked the code from the backup its the same and also copied original file but nothing worked for me.
What is wrong with this file? Can anybody help?
below is the code of Abstract.php file:
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license#magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* #category Mage
* #package Mage_Sales
* #copyright Copyright (c) 2013 Magento Inc. (http://www.magentocommerce.com)
* #license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Quote item abstract model
*
* Price attributes:
* - price - initial item price, declared during product association
* - original_price - product price before any calculations
* - calculation_price - prices for item totals calculation
* - custom_price - new price that can be declared by user and recalculated during calculation process
* - original_custom_price - original defined value of custom price without any convertion
*
* #category Mage
* #package Mage_Sales
* #author Magento Core Team <core#magentocommerce.com>
*/
abstract class Mage_Sales_Model_Quote_Item_Abstract extends Mage_Core_Model_Abstract
implements Mage_Catalog_Model_Product_Configuration_Item_Interface
{
/**
* Parent item for sub items for bundle product, configurable product, etc.
*
* #var Mage_Sales_Model_Quote_Item_Abstract
*/
protected $_parentItem = null;
/**
* Children items in bundle product, configurable product, etc.
*
* #var array
*/
protected $_children = array();
/**
*
* #var array
*/
protected $_messages = array();
/**
* Retrieve Quote instance
*
* #return Mage_Sales_Model_Quote
*/
abstract function getQuote();
/**
* Retrieve product model object associated with item
*
* #return Mage_Catalog_Model_Product
*/
public function getProduct()
{
$product = $this->_getData('product');
if ($product === null && $this->getProductId()) {
$product = Mage::getModel('catalog/product')
->setStoreId($this->getQuote()->getStoreId())
->load($this->getProductId());
$this->setProduct($product);
}
/**
* Reset product final price because it related to custom options
*/
$product->setFinalPrice(null);
if (is_array($this->_optionsByCode)) {
$product->setCustomOptions($this->_optionsByCode);
}
return $product;
}
/**
* Returns special download params (if needed) for custom option with type = 'file'
* Needed to implement Mage_Catalog_Model_Product_Configuration_Item_Interface.
* Return null, as quote item needs no additional configuration.
*
* #return null|Varien_Object
*/
public function getFileDownloadParams()
{
return null;
}
/**
* Specify parent item id before saving data
*
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
protected function _beforeSave()
{
parent::_beforeSave();
if ($this->getParentItem()) {
$this->setParentItemId($this->getParentItem()->getId());
}
return $this;
}
/**
* Set parent item
*
* #param Mage_Sales_Model_Quote_Item $parentItem
* #return Mage_Sales_Model_Quote_Item
*/
public function setParentItem($parentItem)
{
if ($parentItem) {
$this->_parentItem = $parentItem;
$parentItem->addChild($this);
}
return $this;
}
/**
* Get parent item
*
* #return Mage_Sales_Model_Quote_Item
*/
public function getParentItem()
{
return $this->_parentItem;
}
/**
* Get chil items
*
* #return array
*/
public function getChildren()
{
return $this->_children;
}
/**
* Add child item
*
* #param Mage_Sales_Model_Quote_Item_Abstract $child
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function addChild($child)
{
$this->setHasChildren(true);
$this->_children[] = $child;
return $this;
}
/**
* Adds message(s) for quote item. Duplicated messages are not added.
*
* #param mixed $messages
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function setMessage($messages)
{
$messagesExists = $this->getMessage(false);
if (!is_array($messages)) {
$messages = array($messages);
}
foreach ($messages as $message) {
if (!in_array($message, $messagesExists)) {
$this->addMessage($message);
}
}
return $this;
}
/**
* Add message of quote item to array of messages
*
* #param string $message
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function addMessage($message)
{
$this->_messages[] = $message;
return $this;
}
/**
* Get messages array of quote item
*
* #param bool $string flag for converting messages to string
* #return array|string
*/
public function getMessage($string = true)
{
if ($string) {
return join("\n", $this->_messages);
}
return $this->_messages;
}
/**
* Removes message by text
*
* #param string $text
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function removeMessageByText($text)
{
foreach ($this->_messages as $key => $message) {
if ($message == $text) {
unset($this->_messages[$key]);
}
}
return $this;
}
/**
* Clears all messages
*
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function clearMessage()
{
$this->unsMessage(); // For older compatibility, when we kept message inside data array
$this->_messages = array();
return $this;
}
/**
* Retrieve store model object
*
* #return Mage_Core_Model_Store
*/
public function getStore()
{
return $this->getQuote()->getStore();
}
/**
* Checking item data
*
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function checkData()
{
$this->setHasError(false);
$this->clearMessage();
$qty = $this->_getData('qty');
try {
$this->setQty($qty);
} catch (Mage_Core_Exception $e) {
$this->setHasError(true);
$this->setMessage($e->getMessage());
} catch (Exception $e) {
$this->setHasError(true);
$this->setMessage(Mage::helper('sales')->__('Item qty declaration error.'));
}
try {
$this->getProduct()->getTypeInstance(true)->checkProductBuyState($this->getProduct());
} catch (Mage_Core_Exception $e) {
$this->setHasError(true)
->setMessage($e->getMessage());
$this->getQuote()->setHasError(true)
->addMessage(Mage::helper('sales')->__('Some of the products below do not have all the required options.'));
} catch (Exception $e) {
$this->setHasError(true)
->setMessage(Mage::helper('sales')->__('Item options declaration error.'));
$this->getQuote()->setHasError(true)
->addMessage(Mage::helper('sales')->__('Items options declaration error.'));
}
if ($this->getProduct()->getHasError()) {
$this->setHasError(true)
->setMessage(Mage::helper('sales')->__('Some of the selected options are not currently available.'));
$this->getQuote()->setHasError(true)
->addMessage($this->getProduct()->getMessage(), 'options');
}
if ($this->getHasConfigurationUnavailableError()) {
$this->setHasError(true)
->setMessage(Mage::helper('sales')->__('Selected option(s) or their combination is not currently available.'));
$this->getQuote()->setHasError(true)
->addMessage(Mage::helper('sales')->__('Some item options or their combination are not currently available.'), 'unavailable-configuration');
$this->unsHasConfigurationUnavailableError();
}
return $this;
}
/**
* Get original (not related with parent item) item quantity
*
* #return int|float
*/
public function getQty()
{
return $this->_getData('qty');
}
/**
* Get total item quantity (include parent item relation)
*
* #return int|float
*/
public function getTotalQty()
{
if ($this->getParentItem()) {
return $this->getQty()*$this->getParentItem()->getQty();
}
return $this->getQty();
}
/**
* Calculate item row total price
*
* #return Mage_Sales_Model_Quote_Item
*/
public function calcRowTotal()
{
$qty = $this->getTotalQty();
// Round unit price before multiplying to prevent losing 1 cent on subtotal
$total = $this->getStore()->roundPrice($this->getCalculationPriceOriginal()) * $qty;
$baseTotal = $this->getStore()->roundPrice($this->getBaseCalculationPriceOriginal()) * $qty;
$this->setRowTotal($this->getStore()->roundPrice($total));
$this->setBaseRowTotal($this->getStore()->roundPrice($baseTotal));
return $this;
}
/**
* Get item price used for quote calculation process.
* This method get custom price (if it is defined) or original product final price
*
* #return float
*/
public function getCalculationPrice()
{
$price = $this->_getData('calculation_price');
if (is_null($price)) {
if ($this->hasCustomPrice()) {
$price = $this->getCustomPrice();
} else {
$price = $this->getConvertedPrice();
}
$this->setData('calculation_price', $price);
}
return $price;
}
/**
* Get item price used for quote calculation process.
* This method get original custom price applied before tax calculation
*
* #return float
*/
public function getCalculationPriceOriginal()
{
$price = $this->_getData('calculation_price');
if (is_null($price)) {
if ($this->hasOriginalCustomPrice()) {
$price = $this->getOriginalCustomPrice();
} else {
$price = $this->getConvertedPrice();
}
$this->setData('calculation_price', $price);
}
return $price;
}
/**
* Get calculation price used for quote calculation in base currency.
*
* #return float
*/
public function getBaseCalculationPrice()
{
if (!$this->hasBaseCalculationPrice()) {
if ($this->hasCustomPrice()) {
$price = (float) $this->getCustomPrice();
if ($price) {
$rate = $this->getStore()->convertPrice($price) / $price;
$price = $price / $rate;
}
} else {
$price = $this->getPrice();
}
$this->setBaseCalculationPrice($price);
}
return $this->_getData('base_calculation_price');
}
/**
* Get original calculation price used for quote calculation in base currency.
*
* #return float
*/
public function getBaseCalculationPriceOriginal()
{
if (!$this->hasBaseCalculationPrice()) {
if ($this->hasOriginalCustomPrice()) {
$price = (float) $this->getOriginalCustomPrice();
if ($price) {
$rate = $this->getStore()->convertPrice($price) / $price;
$price = $price / $rate;
}
} else {
$price = $this->getPrice();
}
$this->setBaseCalculationPrice($price);
}
return $this->_getData('base_calculation_price');
}
/**
* Get whether the item is nominal
* TODO: fix for multishipping checkout
*
* #return bool
*/
public function isNominal()
{
if (!$this->hasData('is_nominal')) {
$this->setData('is_nominal', $this->getProduct() ? '1' == $this->getProduct()->getIsRecurring() : false);
}
return $this->_getData('is_nominal');
}
/**
* Data getter for 'is_nominal'
* Used for converting item to order item
*
* #return int
*/
public function getIsNominal()
{
return (int)$this->isNominal();
}
/**
* Get original price (retrieved from product) for item.
* Original price value is in quote selected currency
*
* #return float
*/
public function getOriginalPrice()
{
$price = $this->_getData('original_price');
if (is_null($price)) {
$price = $this->getStore()->convertPrice($this->getBaseOriginalPrice());
$this->setData('original_price', $price);
}
return $price;
}
/**
* Set original price to item (calculation price will be refreshed too)
*
* #param float $price
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function setOriginalPrice($price)
{
return $this->setData('original_price', $price);
}
/**
* Get Original item price (got from product) in base website currency
*
* #return float
*/
public function getBaseOriginalPrice()
{
return $this->_getData('base_original_price');
}
/**
* Specify custom item price (used in case whe we have apply not product price to item)
*
* #param float $value
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function setCustomPrice($value)
{
$this->setCalculationPrice($value);
$this->setBaseCalculationPrice(null);
return $this->setData('custom_price', $value);
}
/**
* Get item price. Item price currency is website base currency.
*
* #return decimal
*/
public function getPrice()
{
return $this->_getData('price');
}
/**
* Specify item price (base calculation price and converted price will be refreshed too)
*
* #param float $value
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function setPrice($value)
{
$this->setBaseCalculationPrice(null);
$this->setConvertedPrice(null);
return $this->setData('price', $value);
}
/**
* Get item price converted to quote currency
* #return float
*/
public function getConvertedPrice()
{
$price = $this->_getData('converted_price');
if (is_null($price)) {
$price = $this->getStore()->convertPrice($this->getPrice());
$this->setData('converted_price', $price);
}
return $price;
}
/**
* Set new value for converted price
* #param float $value
* #return Mage_Sales_Model_Quote_Item_Abstract
*/
public function setConvertedPrice($value)
{
$this->setCalculationPrice(null);
$this->setData('converted_price', $value);
return $this;
}
/**
* Clone quote item
*
* #return Mage_Sales_Model_Quote_Item
*/
public function __clone()
{
$this->setId(null);
$this->_parentItem = null;
$this->_children = array();
$this->_messages = array();
return $this;
}
/**
* Checking if there children calculated or parent item
* when we have parent quote item and its children
*
* #return bool
*/
public function isChildrenCalculated()
{
if ($this->getParentItem()) {
$calculate = $this->getParentItem()->getProduct()->getPriceType();
} else {
$calculate = $this->getProduct()->getPriceType();
}
if ((null !== $calculate) && (int)$calculate === Mage_Catalog_Model_Product_Type_Abstract::CALCULATE_CHILD) {
return true;
}
return false;
}
/**
* Checking can we ship product separatelly (each child separately)
* or each parent product item can be shipped only like one item
*
* #return bool
*/
public function isShipSeparately()
{
if ($this->getParentItem()) {
$shipmentType = $this->getParentItem()->getProduct()->getShipmentType();
} else {
$shipmentType = $this->getProduct()->getShipmentType();
}
if ((null !== $shipmentType) &&
(int)$shipmentType === Mage_Catalog_Model_Product_Type_Abstract::SHIPMENT_SEPARATELY) {
return true;
}
return false;
}
/**
* Calculate item tax amount
*
* #deprecated logic moved to tax totals calculation model
* #return Mage_Sales_Model_Quote_Item
*/
public function calcTaxAmount()
{
$store = $this->getStore();
if (!Mage::helper('tax')->priceIncludesTax($store)) {
if (Mage::helper('tax')->applyTaxAfterDiscount($store)) {
$rowTotal = $this->getRowTotalWithDiscount();
$rowBaseTotal = $this->getBaseRowTotalWithDiscount();
} else {
$rowTotal = $this->getRowTotal();
$rowBaseTotal = $this->getBaseRowTotal();
}
$taxPercent = $this->getTaxPercent()/100;
$this->setTaxAmount($store->roundPrice($rowTotal * $taxPercent));
$this->setBaseTaxAmount($store->roundPrice($rowBaseTotal * $taxPercent));
$rowTotal = $this->getRowTotal();
$rowBaseTotal = $this->getBaseRowTotal();
$this->setTaxBeforeDiscount($store->roundPrice($rowTotal * $taxPercent));
$this->setBaseTaxBeforeDiscount($store->roundPrice($rowBaseTotal * $taxPercent));
} else {
if (Mage::helper('tax')->applyTaxAfterDiscount($store)) {
$totalBaseTax = $this->getBaseTaxAmount();
$totalTax = $this->getTaxAmount();
if ($totalTax && $totalBaseTax) {
$totalTax -= $this->getDiscountAmount() * ($this->getTaxPercent() / 100);
$totalBaseTax -= $this->getBaseDiscountAmount() * ($this->getTaxPercent() / 100);
$this->setBaseTaxAmount($store->roundPrice($totalBaseTax));
$this->setTaxAmount($store->roundPrice($totalTax));
}
}
}
if (Mage::helper('tax')->discountTax($store) && !Mage::helper('tax')->applyTaxAfterDiscount($store)) {
if ($this->getDiscountPercent()) {
$baseTaxAmount = $this->getBaseTaxBeforeDiscount();
$taxAmount = $this->getTaxBeforeDiscount();
$baseDiscountDisposition = $baseTaxAmount/100*$this->getDiscountPercent();
$discountDisposition = $taxAmount/100*$this->getDiscountPercent();
$this->setDiscountAmount($this->getDiscountAmount()+$discountDisposition);
$this->setBaseDiscountAmount($this->getBaseDiscountAmount()+$baseDiscountDisposition);
}
}
return $this;
}
/**
* Get item tax amount
*
* #deprecated
* #return decimal
*/
public function getTaxAmount()
{
return $this->_getData('tax_amount');
}
/**
* Get item base tax amount
*
* #deprecated
* #return decimal
*/
public function getBaseTaxAmount()
{
return $this->_getData('base_tax_amount');
}
/**
* Get item price (item price always exclude price)
*
* #deprecated
* #return decimal
*/
protected function _calculatePrice($value, $saveTaxes = true)
{
$store = $this->getQuote()->getStore();
if (Mage::helper('tax')->priceIncludesTax($store)) {
$bAddress = $this->getQuote()->getBillingAddress();
$sAddress = $this->getQuote()->getShippingAddress();
$address = $this->getAddress();
if ($address) {
switch ($address->getAddressType()) {
case Mage_Sales_Model_Quote_Address::TYPE_BILLING:
$bAddress = $address;
break;
case Mage_Sales_Model_Quote_Address::TYPE_SHIPPING:
$sAddress = $address;
break;
}
}
if ($this->getProduct()->getIsVirtual()) {
$sAddress = $bAddress;
}
$priceExcludingTax = Mage::helper('tax')->getPrice(
$this->getProduct()->setTaxPercent(null),
$value,
false,
$sAddress,
$bAddress,
$this->getQuote()->getCustomerTaxClassId(),
$store
);
$priceIncludingTax = Mage::helper('tax')->getPrice(
$this->getProduct()->setTaxPercent(null),
$value,
true,
$sAddress,
$bAddress,
$this->getQuote()->getCustomerTaxClassId(),
$store
);
if ($saveTaxes) {
$qty = $this->getQty();
if ($this->getParentItem()) {
$qty = $qty*$this->getParentItem()->getQty();
}
if (Mage::helper('tax')->displayCartPriceInclTax($store)) {
$rowTotal = $value*$qty;
$rowTotalExcTax = Mage::helper('tax')->getPrice(
$this->getProduct()->setTaxPercent(null),
$rowTotal,
false,
$sAddress,
$bAddress,
$this->getQuote()->getCustomerTaxClassId(),
$store
);
$rowTotalIncTax = Mage::helper('tax')->getPrice(
$this->getProduct()->setTaxPercent(null),
$rowTotal,
true,
$sAddress,
$bAddress,
$this->getQuote()->getCustomerTaxClassId(),
$store
);
$totalBaseTax = $rowTotalIncTax-$rowTotalExcTax;
$this->setRowTotalExcTax($rowTotalExcTax);
}
else {
$taxAmount = $priceIncludingTax - $priceExcludingTax;
$this->setTaxPercent($this->getProduct()->getTaxPercent());
$totalBaseTax = $taxAmount*$qty;
}
$totalTax = $this->getStore()->convertPrice($totalBaseTax);
$this->setTaxBeforeDiscount($totalTax);
$this->setBaseTaxBeforeDiscount($totalBaseTax);
$this->setTaxAmount($totalTax);
$this->setBaseTaxAmount($totalBaseTax);
}
$value = $priceExcludingTax;
}
return $value;
}
}

Symfony Doctrine One to Many Validation

I'm wanting to validate all the images that are linked to the entity by one to many relationship with the KitchenImage table.
Here's my controller function:
public function addAction(Request $request)
{
// Check for POST
if ($request->getMethod() == 'POST') {
// Load Doctrine
$em = $this->getDoctrine()->getManager();
// Set Validator
$validator = $this->get('validator');
// Kitchen Details
$kitchen = new Kitchen();
$kitchen->setName($request->request->get('name'));
$kitchen->setDescription($request->request->get('description'));
$kitchen->setImage($request->files->get('main-image'));
// Sub Images
foreach ($request->files->get('sub-images') as $subimage)
{
$image = new KitchenImage();
$image->setImage($subimage);
$image->setKitchen($kitchen);
$image->upload();
$em->persist($image);
$kitchen->addImage($image);
}
// Validation
$errors = $validator->validate($kitchen);
if (count($errors) > 0) {
return new Response(print_r($errors, true));
}
else {
$kitchen->upload();
$em->persist($kitchen);
$em->flush();
return new Response('The kitchen is valid! Yes!');
}
}
return $this->render('PWDAdminBundle:Pages:add-kitchen.html.twig');
}
Kitchen Entity (Removed setters and getters for formatting):
/**
* #ORM\Entity
* #ORM\Table(name="kitchen")
*/
class Kitchen
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=100)
* #Assert\NotBlank()
*/
protected $name;
/**
* #ORM\Column(type="text")
* #Assert\NotBlank()
*/
protected $description;
/**
* #Assert\File(maxSize="6000000")
* #Assert\NotNull()
* #Assert\Image(
* minWidth = 800,
* maxWidth = 800,
* minHeight = 467,
* maxHeight = 467
* )
*/
protected $image;
/**
* #ORM\OneToMany(targetEntity="KitchenImage", mappedBy="kitchen")
*/
protected $images;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $imagePath;
public function __construct()
{
$this->images = new ArrayCollection();
}
KitchenImage Entity:
class KitchenImage
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #Assert\File(maxSize="6000000")
* #Assert\NotNull()
* #Assert\Image(
* minWidth = 800,
* maxWidth = 800,
* minHeight = 467,
* maxHeight = 467
* )
*/
protected $image;
/**
* #ORM\ManyToOne(targetEntity="Kitchen", inversedBy="image")
* #ORM\JoinColumn(name="kitchen_id", referencedColumnName="id")
*/
protected $kitchen;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
I have tried the following on the Kitchen Entity:
/**
* #ORM\OneToMany(targetEntity="KitchenImage", mappedBy="kitchen")
* #Assert\All({
* #Assert\Valid
* })
*/
protected $images;
But I get the error message:
"The constraint Valid cannot be nested inside constraint Symfony\Component\Validator\Constraints\All.

symfony2 self referencing entity has validation problems

I have an entity that can have another entity of the same type as parent (self-referencing?).
So I have a container named Sweden, then another Container named Stockholm, and the $parent property of Stockholm is Sweden (these two are the same type of entity).
I have a validation constraint that makes sure no two entities can have the same name, but here arises a problem when I choose a parent for an entity, because the way the validations work seem to be that it then goes on to check not only the name of the entity Stockholm, but also goes to check the entire entity that I chose for parent, and obviously the name Stockholm is already in the DB (otherwise I couldn't have picked it for parent) there is a validation error, seems like a catch22 issue...
some code to illustrate... Any thoughts?
The entity
namespace BizTV\ContainerManagementBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use BizTV\ContainerManagementBundle\Validator\Constraints as BizTVAssert;
use BizTV\UserBundle\Entity\User as user;
use BizTV\ContainerManagementBundle\Entity\Container as Container;
/**
* BizTV\ContainerManagementBundle\Entity\Container
* #BizTVAssert\ContainerExists
* #ORM\Table(name="container")
* #ORM\Entity
*/
class Container
{
/**
* #var integer $id
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string $name
* #Assert\NotBlank(message = "Du måste ange ett namn")
* #ORM\Column(name="name", type="string", length=255, nullable=true)
*/
private $name;
/**
* #var object BizTV\BackendBundle\Entity\company
*
* #ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company")
* #ORM\JoinColumn(name="company", referencedColumnName="id", nullable=false)
*/
protected $company;
/**
* #var object BizTV\ContainerManagementBundle\Entity\ContainerType
*
* #ORM\ManyToOne(targetEntity="BizTV\ContainerManagementBundle\Entity\ContainerType")
* #ORM\JoinColumn(name="container_type", referencedColumnName="id", nullable=false)
*/
protected $containerType;
/**
* #var object BizTV\ContainerManagementBundle\Entity\ContainerSize
*
* #ORM\ManyToOne(targetEntity="BizTV\ContainerManagementBundle\Entity\ContainerSize")
* #ORM\JoinColumn(name="container_size", referencedColumnName="id", nullable=false)
*/
protected $containerSize;
/**
* #ORM\OneToMany(targetEntity="Container", mappedBy="parent")
*/
private $children;
/**
* #ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
* #ORM\OrderBy({"sortOrder" = "ASC"})
* above code does nothing, thought to use that instead of the current jQuery tinySort but aparently not...
*/
private $content;
/**
* #var object BizTV\ContainerManagementBundle\Entity\Container
*
* #ORM\ManyToOne(targetEntity="Container", inversedBy="children")
* #ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
*/
protected $parent;
/**
* #var object BizTV\LayoutManagementBundle\Entity\Layout
*
* #ORM\ManyToOne(targetEntity="BizTV\LayoutManagementBundle\Entity\Layout")
* #ORM\JoinColumn(name="screen_layout", referencedColumnName="id", nullable=true)
*/
protected $screen_layout;
/**
* #ORM\Column(type="boolean", nullable=true)
*
* This only applies to the monitor containerType, others will always have false here.
* The purpose of this bool is the option of never rendering this monitor with a layout (handy for ex. BrfTV servers with lower resolution)
*/
protected $prohibitLayout;
/**
* #ORM\ManyToMany(targetEntity="BizTV\UserBundle\Entity\User", mappedBy="access")
*/
private $users;
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
$this->content = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set company
*
* #param BizTV\BackendBundle\Entity\company $company
*/
public function setCompany(\BizTV\BackendBundle\Entity\company $company)
{
$this->company = $company;
}
/**
* Get company
*
* #return BizTV\BackendBundle\Entity\company
*/
public function getCompany()
{
return $this->company;
}
/**
* Set containerType
*
* #param BizTV\ContainerManagementBundle\Entity\ContainerType $containerType
*/
public function setContainerType(\BizTV\ContainerManagementBundle\Entity\ContainerType $containerType)
{
$this->containerType = $containerType;
}
/**
* Get containerType
*
* #return BizTV\ContainerManagementBundle\Entity\ContainerType
*/
public function getContainerType()
{
return $this->containerType;
}
/**
* Set parent
*
* #param BizTV\ContainerManagementBundle\Entity\Container $parent
*/
public function setParent(\BizTV\ContainerManagementBundle\Entity\Container $parent = NULL)
{
$this->parent = $parent;
}
/**
* Get parent
*
* #return BizTV\ContainerManagementBundle\Entity\Container
*/
public function getParent()
{
return $this->parent;
}
/**
* Set screen_layout
*
* #param BizTV\LayoutManagementBundle\Entity\Layout $screenLayout
*/
public function setScreenLayout(\BizTV\LayoutManagementBundle\Entity\Layout $screenLayout = NULL)
{
$this->screen_layout = $screenLayout;
}
/**
* Get screen_layout
*
* #return BizTV\LayoutManagementBundle\Entity\Layout
*/
public function getScreenLayout()
{
return $this->screen_layout;
}
public function getSelectLabel()
{
if (isset($this->parent)) {
return $this->name . ' (' . $this->parent->getName() . ')';
}
else {
return $this->name;
}
}
/**
* Add users
*
* #param BizTV\UserBundle\Entity\User $users
*/
public function addUser(\BizTV\UserBundle\Entity\User $users)
{
$this->users[] = $users;
}
/**
* Get users
*
* #return Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
/**
* Add children
*
* #param BizTV\ContainerManagementBundle\Entity\Container $children
*/
public function addContainer(\BizTV\ContainerManagementBundle\Entity\Container $children)
{
$this->children[] = $children;
}
/**
* Get children
*
* #return Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Add content
*
* #param BizTV\ContentManagementBundle\Entity\Content $content
*/
public function addContent(\BizTV\ContentManagementBundle\Entity\Content $content)
{
$this->content[] = $content;
}
/**
* Get content
*
* #return Doctrine\Common\Collections\Collection
*/
public function getContent()
{
return $this->content;
}
/**
* Set containerSize
*
* #param BizTV\ContainerManagementBundle\Entity\ContainerSize $containerSize
*/
public function setContainerSize(\BizTV\ContainerManagementBundle\Entity\ContainerSize $containerSize)
{
$this->containerSize = $containerSize;
}
/**
* Get containerSize
*
* #return BizTV\ContainerManagementBundle\Entity\ContainerSize
*/
public function getContainerSize()
{
return $this->containerSize;
}
/**
* Set prohibitLayout
*
* #param boolean $prohibitLayout
*/
public function setProhibitLayout($prohibitLayout)
{
$this->prohibitLayout = $prohibitLayout;
}
/**
* Get prohibitLayout
*
* #return boolean
*/
public function getProhibitLayout()
{
return $this->prohibitLayout;
}
}
Form
namespace BizTV\ContainerManagementBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;
class ContainerBuildingType extends AbstractType
{
function __construct(\BizTV\BackendBundle\Entity\company $company, \BizTV\ContainerManagementBundle\Entity\ContainerType $parentType) {
$this->parentType = $parentType;
$this->company = $company;
}
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text', array('label' => 'Namn på fastighet '));
$parentType = $this->parentType;
$company = $this->company;
$builder->add('parent', 'entity', array(
'label' => 'Välj ett geografiskt område för fastigheten ',
'class' => 'BizTVContainerManagementBundle:Container','property'=>'name',
'query_builder' => function(EntityRepository $er) use ($parentType, $company) {
return $er->createQueryBuilder('u')
->where('u.containerType = :type', 'u.company = :company')
->setParameters( array('type' => $parentType, 'company' => $company) )
->orderBy('u.name', 'ASC');
},
));
}
public function getName()
{
return 'biztv_containermanagementbundle_containerbuildingtype';
}
}
Constraint
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class ContainerExists extends Constraint
{
public $message = 'Namnet är upptaget, vänligen välj ett annat.';
public function validatedBy()
{
return 'containerExists';
}
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}
Validator
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManager as EntityManager;
class ContainerExistsValidator extends ConstraintValidator
{
private $container;
private $em;
public function __construct(Container $container, EntityManager $em) {
$this->container = $container;
$this->em = $em;
}
public function isValid($object, Constraint $constraint)
{
$em = $this->em;
$container = $this->container;
$company = $this->container->get('security.context')->getToken()->getUser()->getCompany();
$parent = $object->getParent();
//Fetch entities with same name in the same container
$repository = $em->getRepository('BizTVContainerManagementBundle:Container');
$query = $repository->createQueryBuilder('c')
->where('c.company = :company and c.parent = :parent')
->setParameters(array('company' => $company, 'parent' => $parent))
->orderBy('c.name', 'ASC')
->getQuery();
$containers = $query->getResult();
foreach ($containers as $g) {
echo "testing ".$g->getName()." against ".$object->getName()."<br/>";
if ( $g->getName() == $object->getName() ) {
$this->setMessage('Namnet '.$object->getName().' är upptaget, vänligen välj ett annat');
return false;
}
}
return true;
}
}
I believe you have your relationships backwards;
Your parent should be OneToMany and your child should be ManyToOne
Unless you intended to have many parents only have 1 child and that child belong to many parents.
The answer to a similar question solved this issue as well:
symfony2 validation of child entity prevents editing of parent entity

Resources