yii2 multi step form - yii2

I have just got accustomed to yii2
Am creating a multistep form in yii2 which involves three related tables
tables
User table:has (idno(primary key), firstname, secondname and lastname)
Education table has: (idno(foreign key), institution_name, year_completed, grade)
Contacts table has: (idno(f.key),contact)
Models and relationships
Relation btwn user and contacts
public function getContacs()
{
return $this->hasMany(Contacts::className(), ['idno' => 'idno']);
}
public function getUser()
{
return $this->hasOne(User::className(), ['idno' => 'idno']);
}
Relationship btwn user and education
public function getEducation()
{
return $this->hasMany(Education::className(), ['idno' => 'idno']);
}
public function getUser()
{
return $this->hasOne(User::className(), ['idno' => 'idno']);
}
How can I create a multistep form that is in the first step a user fills in the his details on the next step on education details the form automatically picks the userid and passes it to the education details until finishing

you need to maintain a flag for each step , for example for first_step = 0 , second_step = 0 third_step = 0 . if first step is filled out change the first_step = 1 do same for other , this fields should be present in user table or you can maintain another table using user id as foreign key . if first_step = 1 then when you login again and page will open where flag = 0 mean it will open the second step directly. This is a basic concept so you can get idea , There are other ways also .

Related

Eloquent retrieve data from another model on runtime

I have two tables;
Data
id
name
Custom_data
id
data_id (references id on Data)
customer_id (references id on Customers)
name
When I retrieve all items from the database (via for example Data::all()) as Customer X then I want to retrieve values from 'Custom_data' in favor of the data in table 'Data' where the customer_id matches X
Example:
Data contains name 'John Doe' with id 1
Custom_data contains a record with data_id 1 and name 'Jane Doe' and customer_id X
When retrieving the models I want to see Jane Doe instead of John Doe. Can this be done on a Model level in Eloquent? This is just a simple example, in our application we have multiple columns that need to be retrieved (firstname, lastname, street, etc. etc.)
How I am currently retrieving the fields is like this:
public function getNameAttribute($name) {
$customData = CustomData::where('customer_id', $this->customer_id)->where('data_id', $this->id)->first();
if(null != $customData) {
return $customData->name;
} else {
return $name;
}
}
Here' how you can do it:
In your Data.php modal file you need to add relationship:
public function CustomData(){
return $this->hasOne(CustomData::class);
}
Now, you can use CustomData function on eloquent record anywhere in Controller or View at runtime to get related data.
Another approach is to get data on condition basis:
$users = User::select('users.id', 'users.username', 'users.active', 'users.parent_id', 'parent.username as parent_username')
->selectRaw("CASE WHEN GROUP_CONCAT(roles.name) = 'student' THEN user_profiles.secondary_email ELSE users.email END as email");
I've used this type of solution for another purpose where I needed to use email on condition basis.
first you need to define relation in model
class DataModel extends Model{
...
public function customData()
{
return $this->hasMany(CustomDataModel::class,"data_id");
}
}
now you have access to this data.
$data = DataModel::with("customData")->first();
$data->name; // John Doe
$data->customData->name; // Jane Doe
Allright, I think I nailed this one.
I made a hasOne relation in my Data model:
public function custom_data() {
return $this->hasOne('App\Models\CustomData', 'data_id')->where('customer_id', $customer_id);
}
After that, I could fairly easily add the correct accessors like so:
public function getNameAttribute($name) {
return null != $this->custom_data ? $this->custom_data->name : $name;
}
If the custom data attribute has been set, we'll return that. If not, we'll return the original attribute.

Access non associated tables in cakephp3 controller

I am a newbie to cakephp3, I am trying to access a "table A" which is not linked to "table B" in the controller of table 2
I want to user category table in UsersController. How to achieve that?
e.g.
user table has three fields id, name, role
category table has 2 fields id, name
articles table has 3 fields id, user_id, category_id, article
In your controller you can use loadModel() to load another table that is not linked to your controller's default model.
class UsersController extends AppController {
public function initialize() {
parent::initialize();
// You don't have to put it in initiliaze
// but if you want to use it in more than one method it's a good place
$this->loadModel('Categories');
$this->loadModel('Articles');
}
public function index() {
// Categories is now available as $this->Categories thanks to loadModel()
$categories = $this->Categories->find()->select(['id', 'name']);
}
}

Relationships between tables in laravel using backpack package

I am using backpack CRUD package to create my website project in laravel 5.2
I want to establish a relationship between two tables. First table is called customer and second table is called transaction. Each customer has many transaction(1:N relationship).
Customer table record:
ID Name
123456 xyz
Transaction table record:
ID CustomerID
101010 123456
I know that I have to specify the relation in the customer model. But, how can I display the result of the relationship in CRUD ?
You should have relationships on both the Transaction and the Customer models, so you can do $customer->transactions and $transaction->customer:
class Customer extends Model
{
/**
* Get the comments for the blog post.
*/
public function transactions()
{
return $this->hasMany('App\Transactions', 'CustomerID', 'ID');
}
}
and
class Transaction extends Model
{
/**
* Get the comments for the blog post.
*/
public function customer()
{
return $this->belongsTo('App\Customer', 'CustomerID', 'ID');
}
}
Spend some time in the Eloquent Relationships Documentation. It's really important to understand them if you want to be a Laravel developer.
In order to display the relationship in the CRUD, you can then use Backpack's select column type to display it in the table view and select or select2 field types to display it in the add/edit views. Read the CRUD Example Entity to better understand how that works.
First of all when you are creating migrations for both tables, table which contain Foreign Key (FK) must have field like this:
public function up(){
$table->increments('id');
$table->integer('customerID')->unsigned();
}
After that you are need to call next command into console
php artisan migrate
Next is going next commands:
php arisan backpack:crud customers
php arisan backpack:crud transactions
After that you need to define functions in models which returns values from other tables. Customer models need to have next function
public function transactions(){
return $this->hasMany('Transaction');
}
Transaction model must have next function
public function customer() {
return $this->belongsTo('Customer');
}
Next you must add CRUD field in Customer controller to display
transactions in select box.
$this->crud->addField([
'label' => 'Transactions', // Label for HTML form field
'type' => 'select2', // HTML element which displaying transactions
'name' => 'customerID', // Table column which is FK for Customer table
'entity'=> 'customer', // Function (method) in Customer model which return transactions
'attribute' => 'ID', // Column which user see in select box
'model' => 'Transaction' // Model which contain FK
]);
Hope this helps :)
After you built onetomany relationship with transaction, you can get the results.
$customer=Customer::where(['id'=>'123456'])->with('transaction')
->first();
print_r($customer->Name); // gives the customer name
foreach($customer->transaction as $cid)
{
print_r($cid->CustomerID); // gives the customer id
}
Laravel Relationships Documentation is always helpful. Go through it.

Laravel 5.2 How to get values from two or more table from a relationship

I have a default authentication table user and another table user_profiles
user(id,email,password)
user_profiles(id,first_name,last_name,mobile)
i am connecting these two table using a many-to-many relationship
for this, i added relationship in the both model class- User and UserProfile
// User Model
public function userProfiles()
{
return $this->belongsToMany('App\Models\UserProfile', 'user_user_profile',
'user_profile_id', 'user_id');
}
//UserProfile Model
public function users()
{
return $this->belongsToMany('App\User', 'user_user_profile',
'user_profile_id', 'user_id');
}
and i tried to access the UserProfle details via user table using
$user=\App\User::find(1)->userProfiles()->get();
but not working and also tried
/$user = \App\User::findOrFail(1)->with('userProfiles')->get();
that is also not working , please help to
Get the user_profile table details along with user table
How to access the Pivot table(user_id,user_profile_id) value
How to display these data from multiple tables into a view form?
You have defined the relationship wrong in your User Model: swap user_id and user_profile_id
// User Model
public function userProfiles()
{
return $this->belongsToMany('App\Models\UserProfile', 'user_user_profile',
'user_id' , 'user_profile_id');
}

cakephp, validation checking a value in a separate table

I'm trying to create a site where, when we send an invoice, the relationship between sender and receiver must be active. This is done in a separate table and requires a boolean value of 1 in the other table.
The invoice will be saved in the table invoices with the following structure:
id
sender
receiver
amount
date due
the relationship table has the following structure:
id
partyone
partytwo
active
partyone and partytwo reference the users table.
The users table has the following structure:
id
username
password
What I'm trying to see is, if the relationships table contains partyone and partytwo AND the boolean is equal to one.
This is the function I have in my invoice table to add a new invoice. It can't be added to the database before a user is in an active relationship with that user.
public function addinvoice(){
if($this->request->is('post')) {
$this->Invoice->create();
if (0 === $this->relationship->find('count',array('conditions'=>array('activate'=>1,'relationship'=> $relationship)))) {
$this->Session->setFlash('Sorry, you do not have an active relationship.');
$this->redirect($this->referer());
}
if ($this->Invoice->save($this->request->data)){
$this->Session->setFlash('The invoice has been saved');
}
} else {
$this->Session->setFlash('The invoice could not be saved. Please, try again.');
}
}
This is the simple answer to your question.
Modify your find like this:
$relationship = $this->relationship->find('count', array(
'conditions' => array(
'partyone' => $user_one_id,
'partytwo' => $user_two_id,
),
));
if (!$relationship) {
//Error here
} else {
//Save here
}