FilesSize validator in Model using factory - file

That's my model SiteDesign.php in Object\Model:
namespace Object\Model;
use Zend\Validator\StringLength;
use Zend\Validator\NotEmpty;
use Zend\Validator\File\Size;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class SiteDesign {
public $logo_filename;
public $background_image;
public $background_color;
public function exchangeArray($data) {
$this->logo_filename = (isset($data['logo_filename'])) ? $data['logo_filename'] : null;
$this->background_image = (isset($data['background_image'])) ? $data['background_image'] : null;
$this->background_color = (isset($data['background_color'])) ? $data['background_color'] : null;
}
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception('Not used');
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'logo_filename',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'FilesSize',
'options' => array(
'max' => 4 * 1024,
'messages' => array(
Size::TOO_BIG => 'Размерът на логото не може да надвишава 5MB.',
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'background_image',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'FilesSize',
'options' => array(
'max' => 4 * 1024,
'messages' => array(
Size::TOO_BIG => 'Размерът на изображението за фон не може да надвишава 5MB.',
),
),
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'background_color',
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
but there's an error message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for FilesSize
so is it possible to define a FilesSize validator directly in the model using factory or not? And is this the right way to do it? Thanks in advance.

As of Zend\Validator\ValidatorPluginManager (which is responsible of instantiating validators by name) the correct name is `"FileSize" (you got too many "s" in there).

Related

CakePHP 2.x retriving data with i18n in a loop

I'm looping trough Features (belongTo FeatureType) and find() FeatureType.name for each Feature.feature_type_id.
I'm getting first record correct (with i18n translation) but in all the rest the i18n translation is not in place, but given as separate record.
What I am doing wrong?
this is the debug of my result table:
array(
'FeatureType' => array(
'id' => '28',
'name' => 'kolor suwaka',
'comment' => 'kolor suwaka etui notebook',
'locale' => 'pol'
)
)
array(
'FeatureType' => array(
'id' => '7',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor materiału',
'FeatureType__i18n_comment' => 'gra w klasy'
)
)
array(
'FeatureType' => array(
'id' => '11',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor',
'FeatureType__i18n_comment' => 'kółko i krzyżyk (jasnoszare i ciemnoszare)'
)
)
array(
'FeatureType' => array(
'id' => '27',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'obwód głowy',
'FeatureType__i18n_comment' => 'rozmiar kapelusza czarownicy'
)
)
This is the code:
$features_str = "";
if (!empty($product['Features'])) {
foreach ($product['Features'] as $featureIndex => $feature) {
$featureTypeModel = new FeatureType();
$feature_type = $featureTypeModel->find("first", array("conditions" => array("FeatureType.id" => $feature['feature_type_id'])));
if (strlen($features_str) > 0) $features_str .= ", ";
$features_str .= $feature_type['FeatureType']['name'] . ': ' . $feature['name'];
unset($featureTypeModel);
}
}

Couldn't find Aco node identified by "Array ( [Aco0.model] => model [Aco0.foreign_key] => U ) "

I'm going through the following links to use Acl component in my application
http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html
http://code.tutsplus.com/tutorials/how-to-use-cakephps-access-control-lists--net-1345
in usersController i have a function to install aros and acos
public function install(){
$aro = $this->Acl->Aro;
$aco = $this->Acl->Aco;
$aro_groups = array(
0 => array(
'alias' => 'admin'
),
1 => array(
'alias' => 'operator'
),
2 => array(
'alias' => 'user'
),
);
$aco_groups = array(
0 => array(
'alias' => 'User'
),
1 => array(
'alias' => 'Supplier'
),
2 => array(
'alias' => 'Inventory'
),
3 => array(
'alias' => 'Invoice'
),
4 => array(
'alias' => 'Incentive'
),
5 => array(
'alias' => 'Promotion'
),
6 => array(
'alias' => 'Feedback'
),
7 => array(
'alias' => 'Message'
),
8 => array(
'alias' => 'History'
),
);
foreach($aro_groups as $data):
$aro->create();
$aro->save($data);
endforeach;
foreach($aco_groups as $data):
$aco->create();
$aco->save($data);
endforeach;
foreach($aco_groups as $data):
$this->Acl->allow('admin',$data);
$this->Acl->allow('operator',$data);
endforeach;
}
my user model is as follows:
public $belongsTo = array(
'Role' => array(
'className' => 'Role',
'foreignKey' => 'role_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
public function beforeSave($options = array()){
if(isset($this->data['User']['password']))
{
$this->data['User']['password']= AuthComponent::password($this->data['User']['password']);
}
}
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['group_id'])) {
$groupId = $this->data['User']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
public function bindNode($user) {
return array('model' => 'Role', 'foreign_key' => $user['User']['role_id']);
}
role model:
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'role_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
return null;
}
now, when I try to access /users/install just to check that all relations are successfully created or not, i'm getting this error
AclNode::node() - Couldn't find Aco node identified by "Array ( [Aco0.model] => model [Aco0.foreign_key] => U ) "
Warning (2): Illegal string offset 'id' [CORE\Cake\Model\AclNode.php, line 140]
In the parentNode function in your User model, you have group_id instead of role_id.
Replace all occurrences of group_id to role_id in your User model and you should be ok and also Replace Group with Role
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['User']['role_id'])) {
$roleId = $this->data['User']['role_id'];
} else {
$roleId = $this->field('role_id');
}
if (!$roleId) {
return null;
} else {
return array('Role' => array('id' => $roleId));
}
}

How do I query data in CakePHP using HABTM relationships and translation?

I'm using Cakephp and try to make my model to ActAs 'Translation'. But i have a problem.
My Client model relations are:
public $belongsTo = array("User");
public $hasAndBelongsToMany = array(
'Category' =>
array(
'className' => 'Category',
'joinTable' => 'categories_clients',
'foreignKey' => 'client_id',
'associationForeignKey' => 'category_id',
'unique' => true,
),
);
In Category Model:
public $name = 'Category';
public $hasMany = array(
'Product'
);
public $displayField = 'title';
public $recursive = 1;
public $actsAs = array(
'Tree',
'Translate' => array(
'title',
'alias',
'description',
),
'Containable' => array('CategoryTranslations'),
);
public $translateModel = 'CategoryTranslations';
public $translateTable = 'category_translations';
When i query from Client Controller to get all Client and relative Category, i can't get field "title", "alias" and "description" of Category. Here is my code:
if(isset($this->passedArgs['language_id'])){
$language_id = $this->passedArgs['language_id'];
}
else{
$language_id = Configure::read('Config.language_site');
}
$this->Client->Category->locale = $language_id;
$this->Client->recursive = 1;
$this->paginate = array('limit' => 20,'order' => array('User.id' => 'desc'));
$Clients = $this->paginate();
Here is result:
array(
'Client' => array(
'id' => '1',
'user_id' => '1',
'client_type' => 'tenderer',
'image' => null,
'image_list' => null,
'code' => 'SG0593',
'name' => 'Oorts',
'address' => '1000 Huynh Tan Phat',
'telephone' => '0987654321',
'fax' => '0983213434',
'email' => 'nguyentuandat.vn#gmail.com',
'proxy' => 'Dat Nguyen',
'position' => 'C.E.O',
'mobile' => '0987654321',
'referred_by' => 'noone',
'order' => null,
'status' => true,
'created' => '2014-03-27 00:00:00',
'modified' => '2014-03-27 00:00:00'
),
'User' => array(
'password' => '*****',
'id' => '1',
'username' => 'admin',
'group_id' => '1',
'gender' => 'Male',
'first_name' => 'Nguyễn',
'middle_name' => 'Tuấn',
'last_name' => 'Đạt',
'email' => 'nguyentuandat.vn#gmail.com',
'phone_number' => '(+84) 947235313',
'image' => '/uploads/images/255443_102754699884371_1177788671_n.jpg',
'status' => true,
'created' => '2014-01-16 09:26:09',
'modified' => '2014-01-22 06:47:25',
'full_name' => 'Nguyễn Tuấn Đạt'
),
'Category' => array(
(int) 0 => array(
'id' => '1',
'parent_id' => '0',
'type' => 'product',
'image' => '',
'order' => '0',
'status' => true,
'lft' => '1',
'rght' => '16',
'created' => '2014-01-25 00:00:00',
'modified' => '2014-01-25 00:00:00',
'CategoriesClient' => array(
'id' => '1',
'category_id' => '1',
'client_id' => '1'
)
)
)
)
Can you help me? Thank you!
Restructure the code
Unfortunately because of how behaviors work - that's none-trivial to solve exactly as asked.
You can however do something like this:
$Clients = $this->paginate();
// extract category ids in the results
$categoryIds = array_unique(Hash::extract($Clients, '{n}.Category.{n}.id'));
$categories = $this->Client->Category->find('all', array(
'conditions' => array(
'Category.id' => $categoryIds
)
));
// generate a category-id indexed array of categories
$categories = Hash::combine($categories, '{n}.Category.id', '{n}.Category');
// replace the untranslated category entries with translated category data
foreach($Clients as &$client) {
foreach($client['Category'] as &$cat) {
$id = $cat['id'];
$cat = $categories[$id];
}
}
This example is shown as pseudo controller code - you could also consider putting it in your Client afterFind method or a custom finder.
Thank for help.
I found a solution:
I added bellow code into AppModel->afterFind and it work fine
public function afterFind($results, $primary = false) {
if(!empty($this->hasAndBelongsToMany)) {
foreach($this->hasAndBelongsToMany as $model => $settings) {
if(isset($this->{$model}->actsAs['Translate'])) {
if(!empty($results[0][$model])) {
foreach($results as $k => $v){
foreach($results[$k][$model] as $row => $result) {
$supplement = $this->{$model}->find('first', array(
'conditions' => array(
$model .'.id' => $result['id']),
'fields' => $this->{$model}->actsAs['Translate'],
'recursive' => -1));
if(!empty($supplement)) {
$results[$k][$model][$row] = array_merge($results[$k][$model][$row], array_diff($supplement[$model], $result));
}
}
}// end foreach k=>v
}
}
}
}
return $results;
}

CakePHP - Save fails; validationErrors is empty

So, I have this method where I save a model and its associated models. It has validation rules, but not on the fields that are causing the errors.
The data being passed to the save method is ok, the return of the save method using atomic = false is true on every field except two, that do not have any validation.
The saveAll/saveAssociated fails, and the return of $this->Model->validationErrors is the list of the fields that are not being validated as a empty array.
I already had searched for this on web and found a similar problem to mine here on StackOverflow, but the answer didn't solve my problem.
EDIT
The link to the problem similar to mine: CakePHP save failing with validation errors set but empty
EDIT 2
Here's the code. The rest of the models are ok, so I won't post it here.
**Add Method**
public function add() {
if ($this->request->is('post')) {
if(empty($this->request->data['User']['referer'])) {
$this->request->data['Partner']['status'] = 1;
$this->request->data['User']['group_id'] = 2;
$this->request->data['User']['status'] = 1;
$this->request->data['Customer']['birthday'] = implode("-", array_reverse(explode("/", $this->request->data['Customer']['birthday'])));
$this->request->data['Customer']['newsletter'] = ($this->request->data['Customer']['newsletter'] == "1") ? true : false;
$this->request->data['Customer']['accept'] = ($this->request->data['Customer']['accept'] == "1") ? true : false;
if(!$this->request->data['Customer']['accept']) {
$this->Session->setFlash(__('You must accept the terms and conditions to proceed.'), 'danger');
} else {
$this->Customer->Partner->create();
var_dump($this->Customer->Partner->saveAssociated($this->request->data, array('atomic' => false)));
debug($this->Customer->Partner->validationErrors);
exit();
if($this->Partner->saveAll($this->request->data)) {
$this->Session->setFlash(__('Your profile has been saved. Welcome to Abaixo da Tabela.'), 'success');
$this->redirect(array('controller' => 'pages', 'action' => 'display', 'home', 'admin' => false));
} else {
$this->Session->setFlash('Ocorreu um erro ao tentar salvar seu perfil. Verifique se os campos estão preenchidos corretamente e tenta novamente.', 'danger');
}
}
}
}
}
** Partner Model **
class Partner extends AppModel {
public $validate = array(
'person_type' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'status' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
);
public $hasMany = array(
'Address' => array(
'className' => 'Address',
'foreignKey' => 'partner_id',
),
'Contact' => array(
'className' => 'Contact',
'foreignKey' => 'partner_id',
),
'Customer' => array(
'className' => 'Customer',
'foreignKey' => 'partner_id',
),
'User' => array(
'className' => 'User',
'foreignKey' => 'partner_id',
),
);
}
** Address Model **
class Address extends AppModel {
public $validate = array(
'partner_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'address' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'number' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'zip' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'district' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'city_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'uf' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
);
public $belongsTo = array(
'Partner' => array(
'className' => 'Partner',
'foreignKey' => 'partner_id',
),
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
)
);
}
** Customer Model **
class Customer extends AppModel {
public $validate = array(
'partner_id' => array(
'numeric' => array(
'rule' => array('numeric'),
),
),
'birthday' => array(
'date' => array(
'rule' => array('date'),
),
),
);
public $belongsTo = array(
'Partner' => array(
'className' => 'Partner',
'foreignKey' => 'partner_id',
),
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
)
);
}
** debug($this->Customer->Partner->validationErrors) **
array (size=5)
'Partner' => boolean true
'Address' =>
array (size=7)
'zip' => boolean true
'address' => boolean true
'number' => boolean true
'adjunct' => boolean false
'district' => boolean true
'uf' => boolean true
'city_id' => boolean true
'Contact' =>
array (size=3)
'email' => boolean true
'phone_1' => boolean true
'phone_2' => boolean true
'User' =>
array (size=5)
'username' => boolean true
'password' => boolean true
'password_confirm' => boolean true
'group_id' => boolean true
'status' => boolean true
'Customer' =>
array (size=4)
'genre' => boolean true
'birthday' => boolean true
'newsletter' => boolean false
'accept' => boolean true
Note the two false responses in the return of the save method.
(Answered by a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
Turned out that the error was in the view. The form fields for the associated models were like:
echo $this->Form->input('Customer.birthday')
echo $this->Form->input('Customer.newsletter')
echo $this->Form->input('Customer.accept')
When the correct way was:
echo $this->Form->input('Customer.0.birthday')
echo $this->Form->input('Customer.0.newsletter')
echo $this->Form->input('Customer.0.accept')
As in the Book, the fields for models with the hasMany association need a index, in that case, the 0.

Connect to Multiple data bases using Zend Framework 2 and DoctrineORMModule

Hello i'm using Zend Framework 2 and DoctrineORMModule. I need to access to different data bases connections and map two different set of schemas.
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDODblib\Driver',
'params' => array(
'host' => 'HOST',
'port' => '1433',
'user' => 'USER',
'password' => 'PASS',
'dbname' => 'DBNAME',
)
)
)
),
/////////////
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => '127.0.0.1',
'port' => '3306',
'user' => 'root',
'password' => 'root',
'dbname' => 'test',
)
)
),
),
I found this in the documentation:
https://github.com/doctrine/DoctrineORMModule/blob/master/docs/configuration.md#how-to-use-two-connections
But it is not very descriptive.
Can anyone help me?
use Doctrine\DBAL\DriverManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Configuration;
use Doctrine\DBAL\Connection;
/**
* #author Rafał Książek
*/
class DbFactory
{
/**
* #var array
*/
protected $config;
/**
* #return array
*/
public function getConfig()
{
return $this->config;
}
/**
* #param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* Create connection to database
*
* #param string $dbName
* #return \Doctrine\DBAL\Connection
* #throws \InvalidArgumentException
* #throws \Exception
*/
public function getConnectionToDatabase($dbName)
{
$config = $this->getConfig();
if (empty($config['doctrine']['connection']['orm_default']['params'])) {
throw new \InvalidArgumentException('There is insufficient data in the configuration file!');
}
$params = $config['doctrine']['connection']['orm_default']['params'];
$params['dbname'] = $dbName;
$params['driver'] = 'pdo_mysql';
if (!($dbConnection = DriverManager::getConnection($params)))
throw new \Exception('There was a problem with establish connection to client db');
return $dbConnection;
}
/**
*
* #param \Doctrine\DBAL\Connection $dbConnection
* #param \Doctrine\ORM\Configuration $config
* #return \Doctrine\ORM\EntityManager
*/
public function getEntityManager(Connection $dbConnection, Configuration $config)
{
return EntityManager::create($dbConnection, $config);
}
}
How to used:
$applicationConfig = $sm->get('config');
$em = $sm->get('Doctrine\ORM\EntityManager');
$emDefaultConfig = $em->getConnfiguration();
$dbFactory = new DbFactory($applicationConfig);
$anotherConnection = $dbFactory->getConnectionToDatabase('another_db');
$anotherEntityManager = $dbFactory->getEntityManager($anotherConnection, $emDefaultConfig);
$usersOnAnotherDb = $anotherEntityManager->getRepository('Application\Entity\User')->findAll();
Have a look at link and link. Unfortunately the use statements seems to be missing. These are the factories with FQDN:
'factories' => array(
'doctrine.connection.orm_alternative' => new \DoctrineORMModule\Service\DBALConnectionFactory('orm_alternative'),
'doctrine.configuration.orm_alternative' => new \DoctrineORMModule\Service\ConfigurationFactory('orm_alternative'),
'doctrine.entitymanager.orm_alternative' => new \DoctrineORMModule\Service\EntityManagerFactory('orm_alternative'),
'doctrine.driver.orm_alternative' => new \DoctrineModule\Service\DriverFactory('orm_alternative'),
'doctrine.eventmanager.orm_alternative' => new \DoctrineModule\Service\EventManagerFactory('orm_alternative'),
),
If you only want to access another database (on the same server, same user, etc.), you can easily define your tables in the entities like this:
#ORM\Table(name="database.table")
and save yourself a lot of trouble.
U can set this array config for every separate module in your module config file
return array(
'doctrine' => array(
'orm_autoload_annotations' => true,
'connection' => array(
'orm_default' => array(
'configuration' => 'orm_default',
'eventmanager' => 'orm_default',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
)
),
),
'configuration' => array(
'orm_default' => array(
'driver' => 'orm_default',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
'filters' => array()
)
),
'driver' => array(
'orm_default' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\DriverChain',
'drivers' => array()
)
),
'entitymanager' => array(
'orm_default' => array(
'connection' => 'orm_default',
'configuration' => 'orm_default'
)
),
'eventmanager' => array(
'orm_default' => array()
),
'sql_logger_collector' => array(
'orm_default' => array(),
),
'entity_resolver' => array(
'orm_default' => array()
),
'authentication' => array(
'orm_default' => array(
'objectManager' => 'doctrine.entitymanager.orm_default',
//'identityClass' => 'Application\Model\User',
//'identityProperty' => 'username',
//'credentialProperty' => 'password'
),
),
),
// zendframework/zend-developer-tools specific settings
'view_manager' => array(
'template_map' => array(
'zend-developer-tools/toolbar/doctrine-orm' => __DIR__ . '/../view/zend-developer-tools/toolbar/doctrine-orm.phtml',
),
),
'zenddevelopertools' => array(
'profiler' => array(
'collectors' => array(
'orm_default' => 'doctrine.sql_logger_collector.orm_default',
),
),
'toolbar' => array(
'entries' => array(
'orm_default' => 'zend-developer-tools/toolbar/doctrine-orm',
),
),
),
);

Resources