Hey have got a page which allows businesses to send invoices to customers (both individuals and other businesses). The Receiver input field allows the user to select which customer to send the invoice to. At the moment the drop down lists businesses, but we want that list to include individuals (User model/table) too. So if the account (Account model/table) doesn't have a company_name then display the user's username.
Below is what I have tried to try and get it working, doesn't give errors, but doesn't show anything on the drop down list. How could I achieve what we want?
Invoice Controller:
$name = "";
if ('Account.company_name' != null) {
$name = 'Account.company_name';
} else {
$name = 'User.username';
}
$accounts2 = $this->User->find('list', array(
'fields' => array('account_id'), 'conditions' => array(
'id' => $this->Auth->user('id'))));
$accounts = $this->User->Relationship->find('list', array(
'fields' => array('receiver_id'),
'conditions' => array('sender_id' => $accounts2)));
$receivername = $this->Account->find('list', array(
'fields' => array($name),
'conditions' => array(
'id' => $accounts)));
View:
echo $this->Form->input('receiver_id', array(
'label' => 'Receiver: ',
'type' => 'select',
'options' => $receivername));
'Account.company_name' != null
is obviously always true, as a string is not null. So
if ('Account.company_name' != null) {
$name = 'Account.company_name';
} else {
$name = 'User.username';
}
always do $name = 'Account.company_name';
you can define a temporary virtual field 'display_name' and code like below
$this->Account->virtualFields['display_name'] = "IF(Account.company_name='' OR Account.company_name IS NULL, (SELECT u.name FROM users u WHERE u.account_id = Account.id),Account.company_name)";
$list = $this->Account->find('list',array(
'fields' => array('Account.id','Account.display_name')
));
Related
I am stuck with this problem. I have two tables users and user_credentials which are related to each other by Id column of the users table. Since I'm importing data from csv file, I'm using the insert() method to insert data in the users table instead of create() inside loop (which is a little bit faster I guess).
Is there any way to get the user id of the user for user_credentials table so that I'll be able to use insert() in this table as well
// begin DB transaction
DB::beginTransaction();
// loop through the data
foreach ($data as $key => $value) {
// eliminate the first row
if ($key == 0)
continue;
// check for phone number validation
if (!isset($value[2]) || $value[2] != null || $value[2] != '')
$phoneNumberValidation = null;
// phone number validation
$phoneNumberValidation = $this->validateNumber($value[2]);
if ($phoneNumberValidation != false) {
$user = $this->user::Where('personal_number', $phoneNumberValidation)
->first();
if ($user)
continue;
}
// create the user
$users[] = [
'first_name' => $value[0],
'last_name' => $value[1],
'username' => 'oisf' . $userId++,
'email' => $value[9],
'password' => Hash::make($value[3]),
'address' => $value[4],
'personal_number' => $phoneNumberValidation,
'post_address' => $value[5],
'grad' => $value[6],
'forb' => $value[7],
'offk' => (empty(trim($value[8]))) ? 0 : $value[8],
'approval_date' => ($value[10] != '') ? Carbon::parse($value[10])->startOfDay()->format('Y-m-d H:i:s') : Carbon::now()->format('Y-m-d H:i:s'),
'samtycke' => false,
'status' => 'approved',
'role_id' => ($role) ? $role->id : $newRole->id,
'member' => ($value[10] != '') ? $value[10] : Carbon::now()->format('Y-m-d'),
'note' => $value[11],
'payment_status' => isset($value[12]) && !empty($value[12]) ? $value[12] : 'unpaid',
'username_id' => $userId
];
// I need to change this
UserCredential::create([
'user_id' => 'i want user id here',
'some_column_name' => $value['some_key']
]);
// to this
$userCredentials[] = [
'user_id' => 'i want user id here',
'some_column_name' => $value['some_key']
];
}
$this->user::insert($users);
// and then after above insert, I would like to use
UserCredential::insert($userCredentials);
// commit the transaction
DB::commit();
Try use DB::getPdo()->lastInsertId();, it will get the last inserted id, so if you use that before inserting into UserCredential, you should get the last id.
I have a Category table with a name field.
I want to be able to overwrite that name using a PartnerCategory Table and join it.
So usually I would get the Category like this:
$options = array(
'contain' => array('PartnerCategory'),
'conditions' => array(
'Category.slug' => $slug,
'Category.status' => true,
)
);
$category = $this->Category->find('first', $options);
And then check, if ParentCategory.name is not empty and replace the Category.name with it.
Overwriting the name with php is tedious so I checked virtual fields.
I can overwrite the Category.name using this:
$this->virtualFields = array(
'name' => 'SELECT name FROM app_partner_categories WHERE category_id = 1 AND partner_id = 60'
);
But if the name in the PartnerCategory table is empty or no mathes are found, the Category.name would be null.
How can I overwrite Category.name only if PartnerCategory.name is found?
Not sure but this could be a possible solution:
$joins = array(array('table' => 'app_partner_categories',
'alias' => 'PartnerCategory',
'type' => 'INNER',
'conditions' => array(
'PartnerCategory.category_id = Category.id'
)
)
);
$options = array(
'fields' => array('IF(PartnerCategory.name IS NULL or PartnerCategory.name = "", Category.name, PartnerCategory.name) as overwritten_name'),
'joins' => $joins,
'conditions' => array(
'Category.slug' => $slug,
'Category.status' => true,
)
);
$category = $this->Category->find('first', $options);
My table structure is below
recipies
categories
ingredients(contain category_id)
recipies_categories
recipies_ingredients
Using cakephp I want to retrieve ingredients of a specified category of a specified recipe. For example I have-
recipies:recp1,recp2,recp3
categories:cat1,cat2,cat3
ingredeints:ing1,ing2,ing3
recipies_categories:recp1-cat1,recp2-cat2,recp2-cat3,recp3-cat3
ingredients_categories:ing1-cat1,ing2-cat2
Given recipe=recp2, category=cat1 or cat3 I should get no ingredients
Given recipe=recep2, category=cat2 I should get ing2
I have tried various ways using joins but couldn't get the result. Anybody can help?
$settings = array();
$settings['recursive'] = 1;
if (!empty($selected_categories)) {
$this->Ingredient->Behaviors->load('Containable');
$settings['conditions'] = array(
'Ingredient.category_id' => $selected_categories //array containing selected catagory ids
);
$settings['contain'] = array(
'Category' => array('id', 'name')
);
}
$settings['joins'] = array(
array('table' => 'recipes_ingredients',
'alias' => 'RecipesIngredient',
'type' => 'INNER',
'conditions' => array(
'RecipesIngredient.ingredient_id = Ingredient.id',
'RecipesIngredient.recipe_id' => $recipe_id //selected recipe
)
)
);
$this->Paginator->settings = $settings;
$ingredients = $this->Paginator->paginate($this->Ingredient);
I am purely new to cakephp and currently working on a project that is built in version 1.3. Basically I am trying to display the city names of the providers which are inserted in the database.
I have two models : gal_store.php and gal_location.php. In the gal_store model, the stores names are saved with their corresponding city ids in city field in gal_stores table. The table gal_locations contains all the cities and their names.
So I tried to JOIN the two tables as below :
var $hasOne = array(
'GalLocation' => array(
'className' => 'GalLocation',
'foreignKey' => 'id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
function getList($limit = 50,$whether_list = false){
$recursive = -1;
$conditions = array("GalStore.city"=> "GalLocation.id");
//$conditions = "";
$order = array("GalStore.address");
if($whether_list == true){
return $this->find("list",array("DISTINCT GalStore.city","recursive"=>$recursive,"limit"=>$limit,"order"=>$order,"conditions" => $conditions));
}else{
return $this->find("all",array("DISTINCT GalStore.city","recursive"=>$recursive,"limit"=>$limit,"order"=>$order,"conditions" => $conditions));
}
}
But in the ctp file when I do a var_dump($gal_locations); it always shows empty ! What is the reason ?
If the gal_locations has one to one relationship with gal_stores, use below code:
var $hasOne = array(
'GalLocation' => array(
'className' => 'GalLocation',
'foreignKey' => 'city',//if the city field contains the id of gal_locations table
'conditions' => '',
'fields' => '',
'order' => ''
),
);
If the gal_locations has one to many relationship with gal_stores, use below code:
var $belongsTo = array(
'GalLocation' => array(
'className' => 'GalLocation',
'foreignKey' => 'city',//if the city field contains the id of gal_locations table
'conditions' => '',
'fields' => '',
'order' => ''
),
);
I have two tables named login and userDetail
Login
login_id
uname
pswd
userdetail_id
and
userdetails
userdetail_id
name
address
email
the login table contain userdetails_id in the userDetail table. i want to get all data from Login table and userDetail table and save it to a variable
if anyone knows, please answer me......
First of all your table structure must be as below.
logins Table.
Id auto_increment
username
password
userDetails Table.
Id auto_increment
user_id
name
address
etc...
Now model for each table would be.
Login
<?php
class Login extends AppModel
{
var $name = 'User';
var $hasMany = array
(
'UserDetail' => array
(
'className' => 'UserDetail',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
}
?>
UserDetail
<?php
class UserDetail extends AppModel
{
var $name = 'UserDetail';
var $belongsTo = array
(
'User' => array
(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => ''
)
}
?>
And finally in controller where you need to fetch login detail.
$login_detail = $this->Login->find('all');
You will see userDetail table records in resulting $login_detail.
use pr($login_detail); in controller to see it in action.
Cheers.
Feel Free to ask.
Make sure ContainableBehavior has been enabled. After that you can use following query:
$login = $this->Login->find('first', array(
'contain' => array(
'Userdetail.userdetail_id'
'Userdetail.name',
'Userdetail.address',
'Userdetail.email'
),
'fields' => array(
'Login.login_id'
'Login.uname',
'Login.pswd'
),
'conditions' => array(
'Login.login_id' => 1
)
));
The query for this task would be:
SELECT Login.*, name,address,email
FROM Login JOIN userdetails
ON Login.userdetail_id=userdetails.userdetail_id
The results of this query could be saved to variables by looping in cakephp.