GroceryCRUD : How to set field dropdown - php

I just code with GroceryCRUD,
This is my Database:
a_guest_data
guest_no
register_date
name
gender
birthday
address
city
country
phone
email
ref
status
date_modified
a_table_data
id_table
tbl_name
group_name
status
seat
party_date
locked
pos_tbl
date_modified
a_table_group
id_table
guest_no
priority
This is my Code for the Controller:
public function guest_management()
{
$crud = new grocery_CRUD();
$crud->set_table('a_guest_data');
$crud->set_subject('Guest');
$crud->set_relation_n_n('tables', 'a_table_group', 'a_table_data', 'guest_no', 'id_table', 'tbl_name');
$crud->unset_columns('register_date','layout_no', 'date_modified', 'ref');
$crud->fields('name', 'register_date', 'gender', 'birthday', 'tables', 'address', 'city', 'country', 'phone', 'email', 'ref', 'status' );
$crud->field_type('country','dropdown', array('Indonesia' => 'Indonesia', 'Others' => 'Others'));
$crud->field_type('gender','dropdown', array('Male' => 'Male', 'Female' => 'Female'));
$crud->field_type('status','dropdown', array('1' => '1', '2' => '2'));
$output = $crud->render();
$this->_example_output($output);
}
And I have the form like this
In the field Tables, I want to set the dropdown fields that only can choose 1 table (The Fact is, now in the field Tables, I can choose many options table. *See in the picture, I choose Table 1, 45, 6, 34).
How can I do that?

This behavior is due to your database structure. If you only want one table value you need your database structure to be "1-n database relation"
Documentation for about database relationships
What you want to achieve in grocery crud terms is set_relation.
Syntax for set_relation:
void set_relation( string $field_name , string $related_table, string $related_title_field [, mixed $where [, string $order_by ] ] )
Docs for set_relation
In the end your priority field will also be in the table a_guest_data

You are using a multiple select input, try removing MULTIPLE markup.
It depends on what type of select you are using.

Related

Distinct Values in Laravel

Hello Friends I am using the following query :
$cur_date = date('Y-m-d');
$clientTemp = DB::table('clients')->where('quotations.exp_date','<',$cur_date)
->join('quotations','quotations.customer_id','=','clients.id')
->get()
->map(function ($clientTemp) {
return [
'id' => $clientTemp->id,
'hash' => $clientTemp->hash,
'name' => $clientTemp->first_name.' '.$clientTemp->last_name,
'email' => $clientTemp->email,
'mobile' => $clientTemp->mobile
];
});
I am getting this data from two tables :
1. Qutoations and 2. Clients.
In quotations table if the exp_date is less than current date then the details will be fetched from client table.
But there is possibility then there are more than 1 rows in quotation table but I want to fetch only one table from that for which customer_id is unique. How can I fetch unique row with same customer_id from quotations table
You'd need to use a GROUP BY clause but due to MySQL's default ONLY_FULL_GROUP_BY mode, you must aggregate any column that has more than one value.
You don't seem to be actually using any values from quotations, so you could just add:
DB::table('clients')->select('clients.*')->groupBy('clients.id')...
Otherwise, you'd need to tell MySQL how to aggregate any rows that have multiple values, like:
DB::table('clients')->selectRaw('clients.*, MIN(quotations.id)')->groupBy('clients.id')...
You should use groupby
$cur_date = date('Y-m-d'); $clientTemp = DB::table('clients')->where('quotations.exp_date','<',$cur_date)
->join('quotations','quotations.customer_id','=','clients.id')
->->groupBy('quotations.customer_id')
->get()
->map(function ($clientTemp) {
return [
'id' => $clientTemp->id,
'hash' => $clientTemp->hash,
'name' => $clientTemp->first_name.' '.$clientTemp->last_name,
'email' => $clientTemp->email,
'mobile' => $clientTemp->mobile
];
});

Insert data to different table in codeigniter

So I have 2 tables.
Table 1: (person_identity)
Id_person(PK), name, address, telp
Table 2: (Student)
Id_person(FK), no_student (PK), status, parent
How to input data to name, address, no_student, parent and status at the same time with the same Id_person but at 2 different table?
You can do like this
$dataPersonIdentity = ['name' => 'Abc', 'address' => 'This is address', 'telp' => 'this is telp'];
$this->db->insert('person_identity',$dataPersonIdentity);
$personIdentityId = $this->db->insert_id();
$dataStudent = ['Id_person' => $personIdentityId, 'no_student' => 8, 'status' => 1, 'parent' => 1];
do insert in the first table (person_identity)
and then do insert in the second table (student)

cant get the group of an int field from a query in cakephp3

I have a table with a field called 'year'. It has many repetitions in the column so I want to find the distinct group. I have 4 different 'years' in about 20 rows and I dont get these values from the query. Instead what is returned are 4 numbers which are not the years (5,14,4,70). The same code worked fine when I used this with suburb field in another table where there were multiple values of this field. I dont get why this isnt working.
//in view
echo $this->Form->input('year', ['label' => 'Year','options' => $allyears]);
//controller
$allyears = $this->TimesheetDates->find('list')
->select(['TimesheetDates.id', 'TimesheetDates.year'])
->group(['TimesheetDates.year'])->autoFields(true)
->order(['TimesheetDates.year'=> 'ASC'])
->hydrate(false);
$this->set('allyears',$allyears);
//another controller and this code worked fine
$suburb = $this->Students->find('list')->where(['Students.address_suburb !=' => '','Students.student_inactive' => 0])
->select(['Students.id','Students.address_suburb'])
->group(['Students.address_suburb'])->autoFields(true)
->order(['Students.address_suburb' => 'ASC'])
->hydrate(false);
take a look at the documentation about how find('list') works
$allyears = $this->TimesheetDates->find('list', [
'keyField' => 'id',
'valueField' => 'year']
)
->group(['year'])
->order(['year'=> 'ASC']);
Note that it has no meaning selecting the id of the TimesheetDates Table as you are grouping by year and the id is choosen randomly between all the records that share the same year

fetch values in listbox from two table using joins

User Table -
column name - location_id
values = 4,5
Location table
column name - id , location_name
values = 4 nagpur
5, Akola
i want to display location names in listbox by joining location_id of user table,
Suppose in location_id column of user table 4,5 values are present then in my select box this only displays nagpur, Akola from location table.
i cannot understand how to explode values of location_id table before select and display only those values which are available in location_id column from location table.
public function getUserLocations() {
$this->loadModel('User');
$getlocations = $this->User->find('list', array(
'fields' => array('Location.id','Location.location_name'),
'joins' => array(
array(
'table' => 'locations',
'alias' => 'Location',
'type' => 'LEFT',
'conditions' => array('FIND_IN_SET(User.location_id,Location.id)')
)
),
));
$this->set('getlocations', $getlocations);
}
below is my code ..
Explode the variable like this :
$getlocations = $this->User->find('list', array(
'fields' => array('Location.id'));
$location_ids[] = explode (',', $getlocations);
Now you will get the data in array format. Modify it according to your needs

Getting the column 'country_id' in 'where' clause is ambiguous - error in search query in cakephp 1.3

I am working with CakePHP 1.3 version for search functionality using Search Plugin.
I have three models:
Demo,
Country
State
Demo has two foreign keys, country_id and state_id. State has the foreign key country_id.
What I am doing is, I have search form which have country & state drop down which fetch all data from countries & states table. When i search any of country from dropdown & submit the form it will show me below error. If i search using only state dropdown i get the correct result.
When I execute the search query, I get the error
'Column 'country_id' in where clause is ambiguous'
My query is:
SELECT `Demo`.`id`, `Demo`.`demo2`, `Demo`.`desc`, `Demo`.`subject`, `Demo`.`gender`, `Demo`.`country_id`, `Demo`.`state_id`, `Demo`.`image_url`, `Country`.`id`, `Country`.`name`, `State`.`id`, `State`.`country_id`, `State`.`description` FROM `demos` AS `Demo` LEFT JOIN `countries` AS `Country` ON (`Demo`.`country_id` = `Country`.`id`) LEFT JOIN `states` AS `State` ON (`Demo`.`state_id` = `State`.`id`) WHERE `country_id` = 2
Model relationships in Demo table:
var $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
The controller query to fetch all Country in dropdown is:
$country=$this->Country->find('list'); //just display the list of country in dropdown
The query search the data from all fields except Country (country_id), because it will not know which country_id it is looking for from table Demo or table State. I need the country_id from the demo table to get the correct result.
As I understand you want to make a find over Demo for a specific country_id.
Well you should define which "country_id" you're using because more than one of those tables
has such a column.
Just use Demo.country_id in the conditions array:
array('conditions' => array('Demo.country_id' => 2));
And you should see some SQL generated by Cake like this:
SELECT `Demo`.`id`, `Demo`.`demo2`, `Demo`.`desc`, `Demo`.`subject`, `Demo`.`gender`, `Demo`.`country_id`, `Demo`.`state_id`, `Demo`.`image_url`, `Country`.`id`, `Country`.`name`, `State`.`id`, `State`.`country_id`, `State`.`description` FROM `demos` AS `Demo` LEFT JOIN `countries` AS `Country` ON (`Demo`.`country_id` = `Country`.`id`) LEFT JOIN `states` AS `State` ON (`Demo`.`state_id` = `State`.`id`) WHERE `Demo`.`country_id` = 2
Try this:
SELECT
Demo.id,
Demo.demo2,
Demo.desc,
Demo.subject,
Demo.gender,
Demo.country_id,
Demo.state_id,
Demo.image_url,
Country.id,
Country.name,
State.id,
State.country_id,
State.description
FROM demos AS Demo
LEFT JOIN countries AS Country ON (Demo.country_id = Country.id)
LEFT JOIN states AS State ON (Demo.state_id = State.id) WHERE Demo.country_id = 2

Categories