as you see on the diagram :
the task belong to a user and a project on the same time.
the user and project can have multiple tasks.
how can i use laravel relationships to create a task?
Nothing comlicated. One task belongsTo project, and also one task belongsTo user.
User hasMany tasks, project hasMany tasks.
Task model:
public function user()
{
return $this->belongsTo(User::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
Project model:
public function tasks()
{
return $this->hasMany(Task::class);
}
Then when you are creating Tasks for project you can do:
$project->tasks()->create([...]);
Related
I have two models Student and StudentRevision with Student model having hasMany relation with StudentRevision model. I have defined a hasMany relation in Student as
public function revisions()
{
return $this->hasMany(
'StudentRevision',
'sid'
);
}
I have a field in students table (Student model) which references current revision of student from student_revisions table.
The table structure is something like this.
students sid srid name ....
student_revisions srid sid batch ....
Now i want to define hasOne relation with StudentRevision model which references current revision linked with Student. Currently I have defined this relation as:
public function current()
{
return $this->hasOne(
'StudentRevision',
'sid'
)
->where('srid', $this->srid);
}
But the problem with this relation is, that $this->srid is not available during query building process and can be only there after the actual model is available.
Please help how to overcome this.
I don't think you can define it as relation. But what you can do is this:
public function current(){
return $this->revisions()->where('srid', $this->srid)->get();
}
This way you can access it by $student->current(). You can even go a bit further and make it more relationship like:
public function current(){
return $this->revisions()->where('srid', $this->srid);
}
public function getCurrent(){
return $this->current()->get();
}
protected $appends = array('current');
Here we define an accessor for our attribute. Laravel Docs (scroll down to the bottom)
We can then use it like this:
$student->current; // retrieves the model
$student->current(); // retrieves an instance of the query builder
I have a problem with my function which is supposed to save data to database, function creates a new object which includes other objects. I am able to save and fetch objects which contain only primitive data types and Strings to Database , so the database and system works in this case. I am using JavaEE and EntityManager (persist).
Any help?
You can define a cascade type on the parent entity. Have a look on the Java EE tutorial (http://docs.oracle.com/javaee/6/tutorial/doc/bnbqa.html#bnbqm)
see the principal below
#Entity
public class Customer {
#OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
public Set<Order> getOrders() {
return orders;
}
...
}
#Entity
public class Order {
#OneToOne
Customer customer;
...
}
I'm a beginner and searched documentation, but can't find this how to do this:
I have two tables, admin and application. Admin can have many applications.
ADMIN:
class Model_Admin extends Model_Table {
public $entity_code='admin';
function init(){
parent::init();
$this->addField('name');
$this->addField('email');
$this->addField('password')->type('password');
$this->addField('active')->type('boolean')->system(true);
$this->addField('super')->type('boolean')->system(true);
$this->addField('created')->type('timestamp')->defaultValue($this->dsql()->expr('now()'))->system(true);
$this->addField('updated')->type('timestamp')->system(true);
$this->hasMany('Application','admin_id');
//$this->hasOne('Application');
$this->addHook('beforeSave',function($m){
$m['updated']=$m->dsql()->expr('now()');
});
}
}
APPLICATION:
class Model_Application extends Model_Table {
public $entity_code='application';
function init(){
parent::init();
$this->addField('name');
$this->addField('fbid');
$this->addField('fbsecret');
$this->addField('active')->type('boolean')->system(true);
$this->addField('created')->type('timestamp')->system(true);
$this->addField('updated')->type('timestamp')->system(true);
}
}
First question, when I generate SQL code (/generate.html) it doesn't produce anything for one to many relationship.
Second, on a page I add CRUD:
$this->add('CRUD')->setModel('Admin');
But there is no hint for any one to many. I would expect it on the add button form, but also there is nothing?
What I want is, that I can add admin, and select which applications belong to it?
in Model_Application
$this->hasOne('Admin');
on Page
$this->add('CRUD')->setModel('Application');
In Edit form of CRUD you will see dropdown with all admins and you'll be able to set admin to each application
I have several projects in CakePHP, and would like to move common code into plugins and use seperate GIT repositories for those.
For example, I created a UserManager plugin which contains MVC for users, groups and permissions.
My problem is: the different projects have different (additional) relations to the models from the plugin. E.g., one project should have "User belongsTo Location" in addition.
I'm now confused how to set this up properly. The manual tells how to override Plugin views, but not how this is done with models and controllers.
How can this be done in a clean way?
You can simply extend the plugin classes and override/add the necessary associations, just like you're probably already doing it with AppModel respectively UserManagerAppModel.
http://book.cakephp.org/2.0/en/plugins.html#plugin-models
Here's a basic example (assuming the user class in the plugin is named User):
App::uses('User', 'UserManager.Model');
class AppUser extends User
{
public $belongsTo = array('Location');
}
Or create the associations dynamically in case there are existing ones that need to be kept:
class AppUser extends User
{
public function __construct($id = false, $table = null, $ds = null)
{
parent::__construct($id, $table, $ds);
$this->bindModel(array('belongsTo' => array('Location')));
}
}
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.