extending model : Access parent attribute from the child - eloquent

have to accessing parent attribute from child
after instanciate objectTwo, $object_two->some_objectone_field get null value instead of expected value
below both objects structure :
object one
<?php namespace Username\Plugin\Models;
use Model;
class ObjectOne extends Model
{
use \October\Rain\Database\Traits\Validation;
public $rules = [
....
];
public $table = 'username_plugin_objectone';
public function __construct()
{
parent::__construct();
}
}
object two
<?php namespace Username\Plugin\Models;
use Username\Plugin\Models\ObjectOne;
class ObjectTwo extends ObjectOne
{
public $rules = [
....
];
public $table = 'username_plugin_objecttwo';
public function __construct()
{
parent::__construct();
}
}
database tables
username_plugin_objectone table :
id
some_objectone_field
...
username_plugin_objecttwo table:
id
objectone_id
some_objecttwo_field
...
what am i going wrong ? thanks by advance

ok, things are more clear, i can't access parent properties from database because the parent $table property is overload by child $table property ...

Related

SYMFONY custom CONSTRAINT -> Pass variable to a custom CONSTRAINT / How CONSTRAINT binded to a form field can OVERRIDE CONSTRAINT in ANNOTATION

My goal: I built a custom constraint in SYMFONY, I needed to pass a variable to that constraint.
The context: The constraint do a check if a value is unique in the DB, if it is not, it raises a CONSTRAINT alert. That works alright when the FORM is used to create a new tuple in the DB but if it is an edit it raises an exception which should be bypass by checking that the value already existing, exists for the tuple Id being edited.
Hence I needed to pass the Id of the tuple being edited to my constraint check.
At first I implemented my custom constraint in my entity:
class MyEntity{
/**
* #MyBundleAssert\CheckValueAlreadyInDB(
* message = "already_exists_in_db",
* fieldToSearch = "my_value",
* tableToSearch = "my_table"
*)
*/
private myValue;
}
As one can see, I did not find a way to implement a way to pass a VARIABLE using the constraint with ANNOTATION. By searching, I understood I could do that by using the __construct() of my custom constraint class:
/**
* #Annotation
*/
class CheckValueAlreadyInDB extends Constraint{
public $message;
public $fieldToSearch;
public $tableToSearch;
public $idToCheck;
public $idToCheckFieldName;
public function __construct($options){
if(count($options)>0){
$this->idToCheck = $options['idToCheck'];
$this->idToCheckFieldName = $options['idToCheckFieldName'];
$this->fieldToSearch = $options['fieldToSearch'];
$this->tableToSearch = $options['tableToSearch'];
$this->message = $options['message'];
}
}
public function validatedBy()
{
return 'validator_check_value_already_in_db';
}
}
And, the ConstraintValidator extended class linked to it:
class CheckValueAlreadyInDBValidator extends ConstraintValidator
{
private $con;
public function __construct($con){
$this->con = $con;
}
public function validate($value, Constraint $constraint)
{
////My stuff to get a record from the DB////
$sel = new PdoSelect($this->con);
$search = $sel->returnRecordsInTableForSpecificKey([$constraint->fieldToSearch],[$value], $constraint->tableToSearch,false);
//////////////////////////////////////////////
$sameId = false;
if($constraint->idToCheck!==null){
$idToCheckInRetrieveRecord = $search->{$constraint->idToCheckFieldName};
$sameId = ($idToCheckInRetrieveRecord==$constraint->idToCheck)?true:false;
}
if($search!=null&&!$sameId){
$this->context->buildViolation($constraint->message)
->setParameter('%string%', $value)
->addViolation();
}
}
}
With service:
validator.unique.check_value_already_in_db:
class: MyBundle\Form\CustomConstraints\CheckValueAlreadyInDBValidator
arguments: ['#doctrine.dbal.default_connection']
tags:
- { name: validator.constraint_validator, alias: validator_check_value_already_in_db }
I my FORM (AbstractType extended class) for the field regarding myValue, I did edit the constraints attribute.
class MyEntityType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options)
{
....
$builder->add('myValue',****Type::class,array(
'constraints' => array(
new CheckValueAlreadyInDB(array(
'idToCheck'=>$options['data']->getId(),
'idToCheckFieldName'=>'id',
'fieldToSearch'=>'my_value',
'tableToSearch'=>'my_table',
'message' => "value_already_exists_in_db"))
)
));
...
}
}
I thought that the CONSTRAINT defined in the buildForm() would override the one defined in the * #MyBundleAssert\CheckValueAlreadyInDB(..) of MyEntity class (which should be the default behaviour). But It did not! I had to delete the ANNOTATION above MyEntity to make the constraint work as defined in the buildForm().
Does anyone know if there is a setting that could permit to have a constraint in a buildForm() overriding one existing as an ANNOTATION in MyEntity, but still let the ANNOTATION above a field in MyEntity be the default behavior? Or is there is a way to pass VARIABLE to ANNOTATIONS?
I found the solution.
My mistake was to try to use constraints in class MyEntityType extends AbstractType:
$builder->add('myValue',****Type::class,array(
'constraints' => array(
new CheckValueAlreadyInDB(array(
'idToCheck'=>$options['data']->getId(),
'idToCheckFieldName'=>'id',
'fieldToSearch'=>'my_value',
'tableToSearch'=>'my_table',
'message' => "value_already_exists_in_db"))
)
));
Update:
DON'T USE IT HERE
Have a look at class-constraint-validator section in the doc.
Implement the ConstraintValidator extended class above the class of the Entity where the validator has to execute its check and not above one attribute of the Entity class. That way one can have access to other attributes of the entity and use it as conditionals in the ConstraintValidator extended class.

Zend DB Table and Model one to one

I have class that representing model of user with foreign key with is id of picture .
class Model_User extends Model_AbstractEntity
{
protected $u_id;
protected $u_email;
protected $u_firstname;
protected $u_lastname;
protected $u_password;
protected $u_salt;
protected $u_created_at;
protected $u_updated_at;
protected $u_fb;
protected $u_status;
protected $u_r_id;
protected $u_p_id;
}
Class with is responsible for picture model look like this:
class Model_Picture extends Model_AbstractEntity
{
protected $p_id;
protected $p_created_at;
protected $p_updated_at;
protected $p_caption;
protected $p_name;
protected $p_basePath;
protected $p_available;
protected $p_u_id;
}
This is only model part with is getting data from database.
Foreing key is u_p_id and key in picture is p_id
My problem is that when doing select() by Zend db table it returning me data with foreign key but how can I know which part of return data is picture part to set the proper picture model.... how to do it in proper way no to do 2 queries one for user and second for picture to create 2 associative objects.
I'm talking now about relation ont to one but maybe will be one to many..
Typically your entity models will not exist in void they will exist in concert with some type of Data Mapper Model. The Mapper will typically be charged with gathering the data from whatever source is handy and then constructing the entity model.
For example I have a music collection that has an album entity:
<?php
class Music_Model_Album extends Model_Entity_Abstract implements Interface_Album
{
//id is supplied by Entity_Abstract
protected $name;
protected $art;
protected $year;
protected $artist; //alias of artist_id in Database Table, foreign key
protected $artistMapper = null;
/**
* Constructor, copied from Entity_Abstract
*/
//constructor is called in mapper to instantiate this model
public function __construct(array $options = null)
{
if (is_array($options)) {
$this->setOptions($options);
}
}
/**
* Truncated for brevity.
* Doc blocks and normal getters and setters removed
*/
public function getArtist() {
//if $this->artist is set return
if (!is_null($this->artist) && $this->artist instanceof Music_Model_Artist) {
return $this->artist;
} else {
//set artist mapper if needed
if (!$this->artistMapper) {
$this->artistMapper = new Music_Model_Mapper_Artist();
}
//query the mapper for the artist table and get the artist entity model
return $this->artistMapper->findById($this->getReferenceId('artist'));
}
}
//set the artist id in the identity map
public function setArtist($artist) {
//artist id is sent to identity map. Can be called later if needed - lazy load
$this->setReferenceId('artist', $artist);
return $this;
}
//each album will have multiple tracks, this method allows retrieval as required.
public function getTracks() {
//query mapper for music track table to get tracks from this album
$mapper = new Music_Model_Mapper_Track();
$tracks = $mapper->findByColumn('album_id', $this->id, 'track ASC');
return $tracks;
}
}
In the mapper I would build the entity model like:
//excerpt from Model_Mapper_Album
//createEntity() is declared abstract in Model_Mapper_Abstract
public function createEntity($row)
{
$data = array(
'id' => $row->id,
'name' => $row->name,
'art' => $row->art,
'year' => $row->year,
'artist' => $row->artist_id,//
);
return new Music_Model_Album($data);
}
to use this method in a mapper method, might look like:
//this is actually from Model_Mapper_Abstract, b ut give the correct idea and will work in any of my mappers.
//this returns one and only one entity
public function findById($id)
{
//if entity id exists in the identity map
if ($this->getMap($id)) {
return $this->getMap($id);
}
//create select object
$select = $this->getGateway()->select();
$select->where('id = ?', $id);
//fetch the data
$row = $this->getGateway()->fetchRow($select);
//create the entity object
$entity = $this->createEntity($row);
//put it in the map, just in case we need it again
$this->setMap($row->id, $entity);
// return the entity
return $entity;
}
I have seen Entities and Mappers built in many different ways, find the method that you like and have fun.
A lot of code has been left out of this demonstration as it doesn't really apply to the question. If you need to see the complete code see it at GitHub.

Multiple forms of same type - Symfony 2

So I have my controller action similar to this
$task1 = new Task();
$form1 = $this->createForm(new MyForm(), $task1);
$task2 = new Task();
$form2 = $this->createForm(new MyForm(), $task2);
And let's say my MyForm has two fields
//...
$builder->add('name', 'text');
$builder->add('note', 'text');
//...
It seems like since the two forms are of the same type MyForm, when rendered in the views, their fields have the same name and IDs (the 'name' fields of two forms share the same name and id; the same goes for the 'note' fields), because of which Symfony may not bind the forms' data correctly. Does anyone know any solution to this?
// your form type
class myType extends AbstractType
{
private $name = 'default_name';
...
//builder and so on
...
public function getName(){
return $this->name;
}
public function setName($name){
$this->name = $name;
}
// or alternativ you can set it via constructor (warning this is only a guess)
public function __constructor($formname)
{
$this->name = $formname;
parent::__construct();
}
}
// you controller
$entity = new Entity();
$request = $this->getRequest();
$formType = new myType();
$formType->setName('foobar');
// or new myType('foobar'); if you set it in the constructor
$form = $this->createForm($formtype, $entity);
now you should be able to set a different id for each instance of the form you crate.. this should result in <input type="text" id="foobar_field_0" name="foobar[field]" required="required> and so on.
I would use a static to create the name
// your form type
class myType extends AbstractType
{
private static $count = 0;
private $suffix;
public function __construct() {
$this->suffix = self::$count++;
}
...
public function getName() {
return 'your_form_'.$this->suffix;
}
}
Then you can create as many as you want without having to set the name everytime.
EDIT: Do not do that! See this instead: http://stackoverflow.com/a/36557060/6268862
In Symfony 3.0:
class MyCustomFormType extends AbstractType
{
private $formCount;
public function __construct()
{
$this->formCount = 0;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
++$this->formCount;
// Build your form...
}
public function getBlockPrefix()
{
return parent::getBlockPrefix().'_'.$this->formCount;
}
}
Now the first instance of the form on the page will have "my_custom_form_0" as its name (same for fields' names and IDs), the second one "my_custom_form_1", ...
create a single dynamic name :
const NAME = "your_name";
public function getName()
{
return self::NAME . '_' . uniqid();
}
your name is always single

select queries with Zend_DB_Table

I have a code something like following
class Application_Model_Company extends Zend_Db_Table_Abstract {
protected $_name = 'companies';
private $id;
private $name;
private $shortName;
private $description;
public static function getAllCompanies() {
$companyObj = new self();
$select = $companyObj->select()->order(array('name'));
$rows = $companyObj->fetchAll($select);
if($rows) {
$companies = array();
foreach($rows as $row) {
$company = new self();
$company->id = $row->id;
$company->name = $row->name;
$company->shortName = $row->short_name;
$company->description = $row->description;
$companies[] = $comapny;
}
// return Company Objects
return $companies;
}else
throw new Exception('Oops..');
}
}
I need to return Company Objects from getAllCompanies() function, But it returns Zend_Db_Table_Row Object. How do I correct this?
Your Model class shouldnt extend the table class. The table class is separate. Your Model should extend the row class if extending anything from Zend_Db at all. Also you shouldnt put retrieval methods on your Model classes directly, they would go on the table classes.
This is because in the paradigm youre trying to use here, a Model represents a single Row of data, the Table class represents the table as a repository of data, and the Rowset class represents a collection of Rows (or Models).
To properly implement what you are describing in your question you would do something like the following:
class Application_Model_DbTable_Company extends Zend_Db_Table_Abstract
{
// table name
protected $_name = 'company';
protected _$rowClass = 'Application_Model_Company';
// your custom retrieval methods
}
class Application_Model_Company extends Zend_Db_Table_Row
{
protected $_tableClass = 'Application_Model_DbTable_Company';
// custom accessors and mutators
}
However, using some kind of implementation of the Data Mapper pattern is whats actually recommended. Check out the Quickstart for a thorough tutorial on a simplified implementation.

Zend Framework Handle One to Many

I have 2 tables "user", "contact",
the relation between two tables is one user has many contact
Table member
m_id
name
------------
Table Contact
c_id
c_m_id
value
in zend model class, i do it in this way
/* Member.php */
class Default_Model_DbTable_Member extends Zend_Db_Table_Abstract
{
protected $_name = 'member';
protected $_dependentTables = array('Default_Model_DbTable_Contact');
}
/* Contact.php */
class Default_Model_DbTable_Contact extends Zend_Db_Table_Abstract
{
protected $_name = 'contact';
protected $_referenceMap = array(
'Member' => array(
'columns'=> array('c_id'),
'refTableClass'=> 'Default_Model_DbTable_Member',
'refColumns'=> array('c_m_id')
)
);
/* IndexController.php */
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$m= new Default_Model_DbTable_Member();
$row = $m->find(1);
$data = $row->current();
$data = $data->findDependentRowset('Default_Model_DbTable_Contact');
print_r($data->toArray());
}
}
But i just get Invalid parameter number: no parameters were bound , my goal is to search a member detail record, and it also contains a array which store all contact info (i can use join method to do that, but i just want to try zend feature)
Have you tried defining
class Default_Model_DbTable_Member extends Zend_Db_Table_Abstract
{
protected $_name = 'member';
protected $_primary = 'm_id'; <---
I've always put the $_dependentTables and $_referenceMap in both connected tables (Contact & Member). Did you try that?