Cakephp Call to undefined method Directory::find() after changing the classname - cakephp

I'm using Cakephp 2.4.
I named one of my table folders (which was a logic name) but I just discover that was a reserved word in cake ... So I tried to change folders to directories (in model/controller/views) but i still get the error
Call to undefined method Directory::find()
My model is now
Directory.php
:
<?php
App::uses('AppModel', 'Model');
/**
* Directory Model
*
* #property Installation $Installation
*/
class Directory extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'name';
public $useTable = 'directories';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Installation' => array(
'className' => 'Installation',
'foreignKey' => 'installation_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
I also change hasMany array in my Installation model
My controller
DirectoriesController.php
<?php
App::uses('AppController', 'Controller');
/**
* Directories Controller
*
* #property Directory $Directory
* #property PaginatorComponent $Paginator
*/
class DirectoriesController extends AppController {
/**
* Components
*
* #var array
*/
public $components = array('Paginator');
}
Can someone tell me where I may have missed something ?
Thank you for your help and sorry for my english.

Related

Laravel 8.1 how to make seeder of user table

I am trying to seed the user table but I am facing some issues can someone guide me where I am missing.
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'#example.com',
'password' => Hash::make('password'),
]);
}
}
I think you are missing the below line
use Illuminate\Support\Str;
The complete code look like
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeders.
*
* #return void
*/
public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'#gmail.com',
'password' => Hash::make('password'),
]);
}
}
Try something like this using eloquent instead of the DB class with an existence check too:
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// Seed test user 1
$seededAdminEmail = 'admin#admin.com';
$user = User::where('email', '=', $seededAdminEmail)->first();
if ($user === null) {
$user = User::create([
'name' => 'Admin',
'email' => $seededAdminEmail,
'password' => Hash::make('password'),
]);
}
// Seed test user 2
$user = User::where('email', '=', 'user#user.com')->first();
if ($user === null) {
$user = User::create([
'name' => 'User',
'email' => 'user#user.com',
'password' => Hash::make('password'),
]);
}
}
}

yii error active record query

i'm trying to use yii and i follow a tutorial to simple create a page that shoe fields of database's table.
i create index view
<?php
/* #var $this yii\web\View */
?>
<h1>articoli/index</h1>
<p>
pippo
<?php
foreach($posts as $post){?>
<h1><?php echo $post->autore; ?> </h1>
<p><?php echo $post->articolo; ?></p>
}
?>
</p>
in controllers i create ArticoliController
<?php
namespace app\controllers;
class ArticoliController extends \yii\web\Controller
{
public function actionIndex()
{
$posts=Articoli::model()->findall();
$data['posts']=$posts;
return $this->render('index',$data);
}
public function actionSaluta(){
$vsa['messaggio']='Alessio';
return $this->render('saluta',$vsa);
}
}
in model i create Articoli .php
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "articoli".
*
* #property integer $id
* #property string $autore
* #property string $articolo
*/
class Articoli extends \yii\db\ActiveRecord
{
/**
* #inheritdoc
*/
public static function tableName()
{
return 'articoli';
}
/**
* #inheritdoc
*/
public function rules()
{
return [
[['articolo'], 'required'],
[['autore'], 'string', 'max' => 55],
[['articolo'], 'string', 'max' => 255],
];
}
/**
* #inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'autore' => 'Autore',
'articolo' => 'Articolo',
];
}
}
when i try it return
PHP Fatal Error – yii\base\ErrorException
Class 'app\controllers\Articoli' not found
i don't understand. I think that it must go to app\models\Articoli.php
I try different way
$posts=Articoli::->findall();
but don't work
Yii2 ActiveRecord don't have static function model(). To fetch all records from Articoli you have to use findAll() static method, or find()->all().
Change usage in controller to:
$posts = Articoli::findAll();
In your controller add use:
use \app\models\Articoli;
Or just change this line:
$posts=Articoli::model()->findall();
to this:
$posts = \app\models\Articoli::findAll();
And that's all! ;)

Symfony 2 override entity field property

I want to override the entity field property. I need to get data from another database table (mapped by id). It should be a combination of "artikelnummer" and a field called "name" from another database table.
$builder->add('schlauch', 'entity', array(
'class' => 'SchlauchBundle:Artikelspezifikation',
'property' => 'artikelnummer',
'attr' => array(
'class' => 'extended-select'
),
'data_class' => null
));
The field "artikelnummer" outputs something like "12345" but I need to add the name (from another database table called "schlauch"), so it should look like "12345 Articlename". I tried a query in the entity file, but I dont't want to manipulate the output everywhere.
Is it possible to use a query for property and override it?
You can simple solve that by adding new getter to you entity:
class Artikelspezifikation
{
//…
/**
* #var Schlauch
*
* #ORM\ManyToOne(targetEntity="Schlauch", inversedBy="artikelspezifikations")
*/
private $schlauch;
//…
/**
* Get display name
*
* #return string
*/
public function getDisplayName()
{
return $this->artikelnummer . ' ' . $this->schlauch->getArtikelName();
}
//…
/**
* Set schlauch
*
* #param \SchlauchBundle\Entity\Schlauch $schlauch
*
* #return Artikelspezifikation
*/
public function setCategory(\SchlauchBundle\Entity\Schlauch $schlauch = null)
{
$this->schlauch = $schlauch;
return $this;
}
/**
* Get schlauch
*
* #return \SchlauchBundle\Entity\Schlauch
*/
public function getCategory()
{
return $this->schlauch;
}
}
And in your form class just change property:
$builder->add('schlauch', 'entity', array(
'class' => 'SchlauchBundle:Artikelspezifikation',
'property' => 'displayName',
'attr' => array(
'class' => 'extended-select'
),
'data_class' => null
));

cakephp Error: Association is already bound to model Indicator TranslateBehavior.php

I have been working with cakePHP 2, recently and I have found some weird errors with the i18n translation when I try to instantiate or manage objects...
For example now I have the following error:
Error: Association IndicatornameTranslation is already bound to model Indicator
File: /home3/siriarte/public_html/css/lib/Cake/Model/Behavior/TranslateBehavior.php
Line: 630
The line that is producing this error is:
$Indicator = new Indicator();
In the following code:
public function getRelatedIndicators($id) {
App::uses('Indicatornode', 'Model');
App::uses('Indicator', 'Model');
$Indicatornode = new Indicatornode();
$r = $Indicatornode->find('all',array(
'conditions'=>array("node_id"=>$id),
'recursive'=>-1
));
if (is_array($r)) {
foreach($r as $r2) {
$re[]=$r2["Indicatornode"]["indicator_id"];
}
}
if (is_array($re)and sizeof($re)) {
$Indicator = new Indicator();
$Indicator->setLanguage();
$rel=$Indicator->find('all', array("conditions"=>array("Indicator.id"=>$re), 'recursive'=>-1, 'order'=>"Indicator.name"));
return $rel;
}
return array();
}
This code is inside a Model called "Node"
The definition of the Model "Indicator" which is the one that gives the error with the translation model (I think) is:
<?php
App::uses('AppModel', 'Model');
/**
* Indicator Model
*
*/
class Indicator extends AppModel {
/**
* Validation rules
*
* #var array
*/
public $actsAs = array(
'Acl' => array('type' => 'requester'),
'Translate' => array(
'name' => 'IndicatornameTranslation',
'metric' => 'IndicatormetricTranslation',
'description' => 'IndicatordescriptionTranslation'
)
);
public $locale = 'esp';
....
Anyone have had a similar error message before in cake?
Any help where to start the debug?
Thanks....

cakephp save related model without foreign key

I would like to create a new model using another parameter
class Beacon extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'name';
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'UUID' => array(
'uuid' => array(
'rule' => array('uuid'),
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasOne associations
*
* #var array
*/
public $hasOne = array(
'Position' => array(
'className' => 'Position',
'foreignKey' => 'beacon_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
I would like to ping this beacon passing in parameter the beacon UUID not the primary key.
class Ping extends AppModel {
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Beacon' => array(
'className' => 'Beacon',
'foreignKey' => 'beacon_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
How do I create a Ping object without the beacon primary_key but the UUID ?
on the ping controller I would like to add
public function pingBeaconWithUUID($uuid) {
How do I manage that ?
Thanks !
If the UUID is unique you can use it as value for the id field. The id needs not to be an integer although it is preferable.
But I would also propose to use the approach by AgRizzo and look up the id.

Resources