cakephp Table for model was not found in datasource default - php

I have just a table on my database named "ficha_seg".
The name of my model file is "Ficha.php" and the name of the controller is "FichasController.php".
So, why i'm getting the error:
Error: Table fichas for model Ficha was not found in datasource default.
after configured my index() method of controller like this:
public function index() {
$this->set('ficha_seg', $this->Ficha->find('all'));
}

By default, the model uses the lowercase, plural form of the model’s class name for the database table name.
If you need to use another table name for your model, you can use the useTable attribute:
class Ficha extends AppModel
{
public $useTable = 'ficha_seg';
}
See http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable
and Model conventions in the Cookbook

To follow CakePHP conventions your table name should be in plural: ficha_segs and your model name should be 'FichaSeg'.
If you don't want to follow it for any reason, do what #nlcO says.

Related

Laravel Model Extending

I have a question about models structure.
I created a model called User.php
Than I would like after get a record from DB initialize another class which extends User class based on the value from DB. I.e. there is a record from DB users
id = 1
name = John
type = 1
If type = 1 I would like to init some other class, i.g. Admin
And folders structure will be
Models
- User.php
- UserTypes
- Admin.php
How it's possible to realize this?
Thanks
You can achieve that by using for polymorphic relation. Let's say a user can be extended by two models "Admin" and "Marketer", they will look something like that:
class Admin extends Model
{
public function user() {
return $this->morphOne('App\User', 'extendable');
}
}
And the User model:
class User extends Model
{
public function extendable() {
return $this->morphTo();
}
}
Of course you will also need to add two columns to your User model extendable_id and extendable_type to hold the relation.
To read more you can check laravel documentation https://laravel.com/docs/5.5/eloquent-relationships#polymorphic-relations

how does a cakePHP controller have a model property built in?

If I have a controller as follows.
<?php
class LoginController extends AppController{
public function index(){
}
}
?>
I could access Login model as $this->Login . How does the LoginController class have access to the Login model? We did not define any property named Login in the LoginController class. How does this happen?
CakePHP will dynamically create a model object for you if it cannot
find a corresponding file in /app/Model. This also means that if you
accidentally name your file wrong (for example, post.php or posts.php
instead of Post.php), CakePHP will not recognize any of your settings
and will use the defaults instead.
Model.php, automatically selects a database table name based on a pluralized lowercase object,The table is required to have at least 'id auto_increment' primary key.
You can see for cakephp/lib/Cake/Model/Model.php

Database Table custom naming convention Laravel Eloquent

I'm trying to retrieve data from a table called completion_date using Eloquent. My Model name is Completion and as per laravel documentation, i should declare a (protected) table name or else Eloquent will use 'completions' as the default table name. So i did this declaration.
I'm getting problems with my Controller since i dont know which name to use to refer to my Model when i'm making the View. I'm getting an InvalidArgumentException that View [completion.lsz] not found. if i just use my model name to make the View.
Error:
InvalidArgumentException thrown with message "View [completion.lsz] not found."
Can someone pls help out?
Model
<?php
//Model (file name: Completion)
class Completion extends Eloquent{
protected $table = 'completion_date';
public $timestamps = false;
}
Controller
class CompletionController extends BaseController {
public function index() {
$lsz = Completion::all();
return View::make('completion.lsz', ['completion' => $lsz]);
}
}
Route
Route::get('/Completion', 'CompletionController#index');
View names in Laravel work like a path. The . gets converted to a /
That means, your view resolves to the file app/views/completion/lsz.blade.php (or app/views/completion/lsz.php without Blade)
So you either have to change the name of your directory in the views folder to "completion" or change the view make command to:
View::make('lsz.lsz', ['completion' => $lsz]);
The error message says that the view file completion/lsz.blade.php was not found.
It's not related to the model or database.

Yii: add prefix/postfix to model class naming

Is there any way I can get Yii to work with models that have a prefix or postfix in their class name?
For example, I have a table user, which corresponds to the model User. Now, I want this model to have a prefix, say, EmulatedUser. Is there any way to achieve that without renaming my table?
The table and class name don't have to be the same. You can override the tableName in your model:
<?php
class EmulatedUser extends CActiveRecord {
public function tableName() {
return 'user';
}
}

Using CakePHP model class

I'm just studying CakePHP so sorry for any obvious mistakes.
I have created model class and changed default table name.
class Weathers extends AppModel {
public $tablePrefix = 'weather_';
public $useTable = 'forecasts';
function saveCountries($countries){
...
}
}
And my controller function
if (!$this->loadModel('Weather'))
exit;
$Weather = $this->Weather;
$Weather->saveCountries($countries);
I'm getting error on $Weather->saveCountries($countries);
Error: Table weathers for model Weather was not found in datasource
default.
Please help find out what I do wrong.
The Model class you defined is Weathers not Weather. So just change the class name Weather instead of Weathers and this is done.
Note the declaration of your Model class.
You've called it Weathers.
There is no problem with this. However, as you are trying to then load the model Weather (not the lack of plural in this case) CakePHP is constructing a model dynamically for you, instead of using your Weathers (plural) class.
The CakePHP standard is to use a singular name for models. I suggest that you rename your model class to Weather to avoid this issue. Once you make this change, the code that you have for loading the model will work as intended.

Categories