I create a Module in Magento, Now i am want to Use the Other model to take collection but thats give me error,
**Error:** There has been an error processing your request
Exception printing is disabled by default for security reasons.
Error log record number: 1685082734
And my Collection class is given below..
class Mage_Banners_Model_Mysql4_Category_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('banners/category');
}
}
So how to run this and how this model resource know about has table..?
If you want to add another model to your custom module, you should enter your table name in config.xml file, which is located in yourmodule/etc folder:
<entities>
<banners>
<table>Your table Name here</table>
</banners>
After that you should add your collection class in corresponding model/mysql4/category/Collection.php. You should create model class in model/file name.
Suppose category.php is the model file, you should initiate that model class by using these methods. These files should be in the model folder:
public function _construct()
{
// Note that the category_id refers to the key field in your database table.
$this->_init('banner/category', 'category_id');
}
Related
This seems extremely simple and yet it doesn't work:
// Reservations
class Reservation extends Model
{
public function service()
{
return $this->belongsTo('App\Landing');
}
}
// Service
class Landing extends Model
{
public function reservation()
{
return $this->hasMany('App\Reservation');
}
}
And then in my controller I have:
$x = Reservation::with('service')->get();
$y = Reservation::all()->load('service');
None of these work. I've tried several ways of loading it and none of them work. It always returns an empty result for the service.
Any other result works fine with eager loading (even with nesting) except te BelongsTo - this one.
The eager loading works.
The problem is your relationship
When you define a BelongsTo relationship you need to specify a foreign key if the name of your property does not correspond to the entity being referenced
For example: if you call the relationship "landing", you will be fine, because under-the-hood, Laravel passes the foreign key landing_id based on the name of the property
class Reservation extends Model
{ //landing correspond to the lowercase (singular) name of the Landing class
//in that case Laravel knows how to set the relationship by assuming
//that you want to match landing_id to the id of the landings table
public function landing()
{
return $this->belongsTo(Landing::class);
}
}
If you chose to name the relationship differently, such as "service", then you need to specify the foreign key ex: landing_id since service and landing are two different words, and landing correspond to the lowercase version of the actual class Landing. Otherwise Laravel would think your foreign key is "service_id" instead of landing_id
class Reservation extends Model
{
//service is a custom name to refer to landing
//in that case Laravel needs you to specify the foreign key
public function service()
{
return $this->belongsTo(Landing::class, 'landing_id');
}
}
Read more here: https://laravel.com/docs/5.8/eloquent-relationships#updating-belongs-to-relationships
I've created a BaseModel class, which extends from Model. It seemed like everything was working fine, but now I've run into a problem when saving. I'm overriding the save() method in this BaseModel. I'd just like to add some attributes to the model before saving. So I do that, then call return parent::save($options);. The method signature is still the same: public function save(array $options = []).
It appears to be grabbing the name of the BaseModel class for the table name when performing the insert (it's using base_models as the table name), rather than the actual model that is being saved. Has anyone run into this before? What is the proper way of extending from the model class?
I originally created some traits to handle some extra functionality, but thought it would be a better idea to just create a base model and have my models extend from that instead.
In your model (the child one that extends the base model) add the table name explictly for example:
class SomeChildModel extends BaseModel {
// Manually set the table name
protected $table = 'table_name';
}
I realized that I previously had a static method that was creating an instance of itself using new self() and would set a few attributes, back when I was using the methods from a trait. It was fine before, but now since I moved the methods into the base model, that method was actually being called on the base model itself rather than the class that had the trait.
I was basically using the static method to instantiate the class, as I've read it's one way to avoid cluttering the constructor. But I just opted to do it in the constructor this time around since it made sense, so that was my solution.
Laravel will use snake case of the class name by default (the class where save method is called), if no $table instance variable is set. In your case it will use snake case of the BaseModel as a table name. You have two solutions:
Solution 1:
In classes which extends BaseModel add the $table instance variable as follow:
class User extends BaseModel {
protected $table = 'table_name'; // Your table name in the database;
}
Solution 2:
You can use Laravel Eloquent's Events, which allows you to hook into various points in the model's lifecycle.
You can hook into the save method as follow and make your changes. You can use these methods in your BaseClass, in traits, etc. For example in your BaseModel:
class BaseModel extends Model
{
/**
* Listen for save event
*/
protected static function boot()
{
parent::boot();
static::saving(function($model)
{
if ( ! $model->isValid()) {
return false;
}
});
}
}
The above will always call isValid before a model is saved into the storage. In this case it will return false and will not save the object.
For more info see the official docs here. Let me know if it isn't clear.
Now I have those business logic, but don't know how to define it in Laravel.
First, there are two basic model named contact and project, and contact and project model owns many-to-many relations so I defined them like below:
class Contact extends Model{
public function projects(){
return $this->belongsToMany('App\Project', 'proj_staff')
->withPivot('role', 'superior');
}
}
And the same as Project:
class Project extends Model{
public function contacts(){
return $this->belongsToMany('App\Contact', 'proj_staff')
->withPivot('role', 'superior');
}
}
As you see, every contact has a superior in his project or not, so the question comes with it that how can I define a relation to get access to superior like this: $contact->projects[0]->superiors, and the superiors has better return a Model Collection rather than just id.
Thx any way.
You are accessing projects from your contacts & you want to access projects & contacts pivot table proj_staff info from your projects model.
So you have to create another relation in your in your Project
model to access the data from your pivot table
class Project extends Model{
public function contacts(){
return $this->belongsToMany('App\Contact', 'proj_staff')
->withPivot('role', 'superior');
}
//your project table has one to many many relationship with proj_staff table
public function superiors(){
return $this->hasMany('App\ProjStaff', 'superior', 'superior');//set relation keys I assume it's superior is local and foreign key
}
}
//Create ProjStaff Pivot Table Model under app directory same level with your Contact model
class ProjStaff extends Model{
//add fillable
//add table name
}
Now you can easily access superior like $contact->projects[0]->superiors->pluck('superior')->all()
Is there any function to get the all table or model names in cakePhp.
I want to update my table's field and for that I need to select all table dynamically so that in future when I'll add new table I do not have to make changes in function.
The new table automatically update the fields.
I don't quite understand the question - from what i gather you want to write abstract actions to account for multiple models? If this is the case you can add abstract actions into the AppController and use $this->modelClass. This will return the model name from were you are calling the abstract action from. For example if you calling the abstract action from 'UsersController' which by default uses the Model 'User' then the modelClass will return 'User'.
class AppController extends Controller {
public function abstractAdd() {
// Get the model in use
$this->{$this->modelClass}->create();
// Use the save method in that model
if ($this->{$this->modelClass}->save($data)) {
// do something
}
}
}
class UsersController extends AppController {
public function add() {
$this->abstractAdd();
}
}
Hope this helps
I too ran into the problem that FUSE won't work with CI and RBphp.
in APPPATH/application/core/ I have a base model called "MY_Model.php"
class MY_Model {}
class Base extends RedBean_SimpleModel {
// static methods
}
I have a derived class, like this, called "model_user.php" in APPPATH/application/models/:
class Model_User extends Base {
public function getData() {
return $this->bean->id;
}
}
And in the controller I load the model:
$this->load->model('Model_User');
The rb library is autoloaded via the autoload.php file.
After creating an object of the type Model_User with redbean, I still cannot access the getData() method.
I already read this question + answer asked here, but they didn't help me.
The problem was due to the naming convention. Whilst the table itself was called "users", the bean's name was "Model_User". So RedBean was looking for a "User" table in the database. Renaming the class to Model_Users, or renaming the table to "user" was the solution.