Laravel Missing Argument db:seed - database

I'm using Laravel. I'm creating my database and seed files. (not using Test Dummy) When I start the process of terminal and i got a Missing Argument errors.
khazax#khz ~/Code/Laravel $ php artisan db:seed
Seeded: UsersTableSeeder
Seeded: RolesTableSeeder
[ErrorException]
Missing argument 1 for Illuminate\Database\Eloquent\Builder::where()
khazax#khz ~/Code/Laravel $
DatabaseSeeder.php
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call(UsersTableSeeder::class);
$this->call(RolesTableSeeder::class);
$this->call(PermissionsTableSeeder::class);
}
}
UsersTableSeeder.php
<?php
use App\Models\User;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
public function run()
{
User::truncate();
User::create([
'first_name' => 'Onur',
'last_name' => 'Kaya',
'username' => 'Khazax',
'username_slug' => 'khazax',
'email' => 'onurkayaes#gmail.com',
'password' => bcrypt('123'),
'confirmation_code' => md5(microtime()+env('APP_KEY')),
'confirmed' => 1,
'settings' => [
'gender' => 'Erkek',
'phone' => '111',
'adress' => 'Eskisehir/Turkiye',
'home_page' => 'https://khazadum.com',
'opt_in_monhtly' => false,
'opt_in_quartly' => false,
'opt_in_year' => true
]
]);
User::create([
'first_name' => 'Tab',
'last_name' => 'Dev',
'username' => 'Tabdev',
'username_slug' => 'tabdev',
'email' => 'info#123.com',
'password' => bcrypt('123'),
'confirmation_code' => md5(microtime()+env('APP_KEY')),
'confirmed' => 1,
'settings' => [
'gender' => 'Erkek',
'phone' => '222',
'adress' => 'Istanbul/Turkey',
'home_page' => 'https://tabtusu.com',
'opt_in_monhtly' => false,
'opt_in_quartly' => false,
'opt_in_year' => true
]
]);
}
}
RolesTableSeeders.php
<?php
use App\Models\Role;
use App\Models\User;
use App\Models\AssignedRole;
use Illuminate\Database\Seeder;
class RolesTableSeeder extends Seeder
{
public function run()
{
Role::truncate();
$adminRole = new Role;
$adminRole->name = 'admin';
$adminRole->display_name = 'Administrator';
$adminRole->description = 'Tüm yetkilerin var olduğu kullanıcı grubu';
$adminRole->is_admin = 1;
$adminRole->save();
$userRole = new Role;
$userRole->name = 'user';
$userRole->display_name = 'User';
$userRole->description = 'Sadece site içi işlemleri yapabilen kullanıcı grubu';
$userRole->is_admin = 0;
$userRole->save();
$admin = User::where('email', 'onurkayaes#gmail.com')->first();
$assRoleAdmin = new AssignedRole;
$assRoleAdmin->user_id = $admin->id;
$assRoleAdmin->role_id = $adminRole->id;
$assRoleAdmin->save();
$user = User::where('email', 'info#123.com')->first();
$assRoleUser = new AssignedRole;
$assRoleUser->user_id = $user->id;
$assRoleUser->role_id = $userRole->id;
$assRoleUser->save();
}
}
and PermissionsTableSeeders.php
<?php
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;
class PermissionsTableSeeder extends Seeder
{
public function run()
{
Permission::truncate();
$permission = [
array(
'name' => 'manage_user',
'display_name' => 'Manage Users',
'is_admin' => 1
),
array(
'name' => 'manage_content',
'display_name' => 'Manage Content',
'is_admin' => 1
),
array(
'name' => 'manage_roles',
'display_name' => 'Manage User Roles',
'is_admin' => 1
),
array(
'name' => 'manage_files',
'display_name' => 'Manage Files',
'is_admin' => 1
)
];
foreach($permission as $row)
{
$row = array_merge($row, ['created_at' => new DateTime, 'updated_at' => new DateTime]);
DB::table('permissions')->insert($row);
}
$role_id_admin = Role::where()->first()->id;
$perm_base = (int)DB::table('permissions')->first()->id-1;
$perms = [
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 1
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 2
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 3
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 4
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 5
)
];
DB::table('permission_role')->delete();
foreach($perms as $row)
{
$row = array_merge($row, ['created_at' => new DateTime, 'updated_at' => new DateTime]);
DB::table('permission_role')->insert($row);
}
}
}
Am I missing some code or is this just a simple typo? Thanks in advance for helping.

The problem in your code is that line:
$role_id_admin = Role::where()->first()->id;
in PermissionsTableSeeder.
Depending on your logic it should look like this:
$role_id_admin = Role::first()->id;
or you should also add condition for this like this:
$role_id_admin = Role::where('name','admin')->first()->id;

Related

how to get the submitted value of tableselect in drupal 7

function test($form, &$form_state){
$form = array();
$header = array(.............);
$values = array(.............);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => $IsCheckbox,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state){
$selected = $form_state['values']['table'];
drupal_set_message($selected) // displays array index (0,1,2 etc)
return;
}
How to get the selected table row values in Drupal form. Need assistance on the issue. Any help would be appreciated.
What you get in your $selected is the index of $rows that you have selected in your table. To get the values in $rows you need to use the index that you have in $selected.
I created an easy example how to do it here:
function test($form, &$form_state)
{
$form = array();
$header = array(
'first_name' => t('First Name'),
'last_name' => t('Last Name'),
);
$rows = array(
// These are the index you get in submit function. The index could be some unique $key in database.
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$form['table'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#multiple' => true,
'#empty' => t('No users found'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
} // end of function test()
function test_submit($form, &$form_state)
{
// This function should not be duplicated like this but It was easier to do.
$rows = array(
'1' => array('first_name' => 'Mario', 'last_name' => 'Mario'),
'2' => array('first_name' => 'Luigi', 'last_name' => 'Mario'),
'3' => array('first_name' => 'Princess Peach', 'last_name' => 'Toadstool'),
);
$names = array();
// Remove the names that has not been checked
$selected_names = array_filter($form_state['values']['table']);
// Iterate over the indexes that was selected to get the data from original array
foreach ($selected_names as $index ) {
array_push($names, $rows[$index]);
}
foreach($names as $name) {
drupal_set_message($name['first_name'] . ' ' . $name['last_name']);
}
}

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;
}

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',
),
),
),
);

FilesSize validator in Model using factory

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).

Resources