Cakephp inheritance of model associations - cakephp

If I create a Model Car with some associations:
hasOne Driver
hasMany Wheel
And then create a Model Truck extending Car with
class Truck extends Car { ... }
is it normal that the associations from Car are not inherited from Truck?
What are the possibilities to inherit model associations?
Thanks in advance!

Inheritance is PHP related; your custom classes will behave the same way regardless of anything in cakephp. Class inheritance in CakePHP is completely depended on How your classes are written. Inheritance is an important part of cakephp; the framework is very much object oriented.
Are you including the classes properly using App::uses() to load your car class into truck?

The only way your Truck class will not inherit from the Car class is if you have defined the properties in the Truck class.
class Car extends AppModel {
public $hasOne = array('Driver', ...);
}
class Truck extends Car {
public $hasOne = array(); // now you have no hasOne relations
}

Related

cakephp Appmodel access child tables

There are a relationship between Appmodel and child model. Is that mean it can access every database table ? However, i cannot access the child database. Is there any difference with the sytax?
App::uses('AppModel', 'Model');
class CompanyAppModel extends AppModel{
$this->Brand; it works
$this->BrandLangauge; it does not work
}
class BrandLanguage extends CompanyAppModel {
}
class Brand extends CompanyAppModel {
}
I'm not clear the question. But, you can use ClassRegistry::init needing to load a model that’s not associated with the current model.
$anotherModel = ClassRegistry::init('AnotherModel');

Exposing a Collection of Abstract Model Classes to View in MVVM

I am struggling with a design desision while writing a WPF application. Here is the generic problem:
Consider an application needing to keep track of a list of abstract Vehicles that a person wants to buy. With a MVVM design, what is the best way to expose the different car types (e.g. Motorcycle, Car, Truck, etc.) to the View so that each of the Vehicle types can be shown and edited with a different form?
Currently I have this setup:
Model
abstract class Vehicle { .. }
class Car : Vehicle { .. }
class Truck : Vehicle { .. }
class Cart {
ObservableCollection<Vehicle> Vehicles;
}
ViewModel
class VehicleViewModel { .. }
class CartViewModel {
ObservableCollection<VehicleViewModel> Vehicles;
}
With this design though, I'm not sure how I can create a DataTemplate in my View of a Cart for each of the Vehicle types because the CartViewModel just contains a list of VehicleViewModel (and not CartViewModel, TruckViewModel, etc).
Can someone please guide me in a direction to take?

How to save inherited objects in cakephp

I have implemented inheritance in cakephp . I have two models Person and Employee. Employee inherits Person
Person: id, name, age, address
Employee: id, person_id
when i do $this->Employee->savAll($this->request->data), Person and Employee are saving but the person_id is not getting set in database, Its being filled by NULL, Is there any way to save the inherited objects properly at one shot, The models are shown below
Class Person extends AppModel{
var $name="Person";
}
App::import('model', 'Person')
Class Employee extends Person{
var $name="Employee";
public $actsAs = array( 'Inherit' );
}
From the Saving Your Data docs,
The saveAll function is just a wrapper around the saveMany and saveAssociated methods
You are saving neither many of one model, nor associated models. You will need to save the parent model, retrieve the ID, and use that to save the inheriting model.

CakePHP: To Create A New Controller

I'm using CakePHP 2.0.5 (but this isn't necessarily a cakephp specific question). I have a Coupon and a User model. Each time a user prints a coupon (proccessed by: Coupon Controller):
class CouponsController extends AppController {
public function printcoupon($id = null) {
// code
}
}
I want to save the information to a "coupons_printed" table (id/coupon_id/user_id/created). Should I create a new model for this, or should I just create a function inside of the Coupon model similar to (and call it in the controller each time that page is viewed)?:
class Coupon extends AppModel {
function insertIntoPrinted($id) {
$this->query("UPDATE coupons_printed SET .....");
}
}
Whatever you do, a raw SQL query is not the best way to go. Always use CakePHP methods if at all possible (and almost always it is possible).
You should put the insertIntoPrinted() function in the CouponsPrinted model (although, as a side note, PrintedCoupon would be a more natural way to name the model...) You can then add a HasMany relationship to the Coupon model ($hasMany = array( 'CouponsPrinted' )) and call the function in the CouponsController:
public function printcoupon($id = null) {
$this->Coupon->CouponsPrinted->insertIntoPrinted( $id );
}
CakePHP's model has a thing call association.
In your case, Coupon has a hasMany association with coupons_printed.
You can create a new model, or query using the association in the Coupon model, the generated queries will be the same, I believe.
Your CouponsController already depend on Coupon Model, so not creating another model is a better solution.

Naming conventions for MVVM?

I'm struggling a little over naming classes for my MVVM application.
I have a TrainingCourse, which is called a TrainingCourseViewModel, but I can have many of these, so I have created a TrainingCourseViewManager to hold the list of courses and allow them to be added/removed. I also have an EmployeeViewController which has a reference to other view models as well as the TrainingCourseViewManager.
The EmployeeViewController essentially wraps all of the other view models and view managers and when its instantiated it gets the employee and in turn instantiates each of the view models and view managers.
The question is... What naming conventions are people using?
Should my TrainingCourseViewManager be called TrainingCoursesViewModel and should my EmployeeViewManager be called EmployeeViewModel?
Thanks
There might be a confusion over the role of view model.
Classes in your example (and in Orion's answer to that matter) seem more like actual data model. For example, it doesn't make sense for a view model to "hold the list of courses and allow them to be added/removed" - that's what data model should do. Add and remove operations on a view model wouldn't operate on the collection itself - instead, they would access and modify underlying data model.
Do properties of TrainingCourseViewModel class store actual data values, or wrap properties of some TrainingCourseDataModel class (with additional processing)? Or if you need to serialize data, would you serialize TrainingCourseViewModel objects? If former is true, you are binding directly to the data model, and there should be no 'ViewModel' suffix in names.
On the topic of naming conventions, if names become too complex, namespaces can help. For example:
namespace TrainingCourseView.ViewModel
{
class TrainingCourse {}
class Manager {}
class Controller {}
}
...
Data.TrainingCourse course;
new ViewModel.TrainingCourse(course);
Should my TrainingCourseViewManager be called TrainingCoursesViewModel and should my EmployeeViewManager be called EmployeeViewModel?
What are your window classes called? (what is your .xaml file called?)
The naming convention goes, that you create one ViewModel class per View (a view is a .xaml/.xaml.cs pair)
If you have a single window which displays a list of Employees and Training Courses, then you'd have something like this:
namespace Models
{
public class Employee : INotifyPropertyChanged { ... }
public class TrainingCourse : INotifyPropertyChanged { ... }
}
namespace ViewModels
{
// assuming you have TrainingWindow.xaml
public class TrainingWindowViewModel : INotifyPropertyChanged
{
public ObservableCollection<TrainingCourse> TrainingCourses
{ get{ return m_trainingCourses; } }
{ set{ m_trainingCourses = value; RaisePropertyChanged("TrainingCourses"); } }
...
}
// so on and so forth
}

Resources