I want to store the csv data dynamically into mysql table. According to my csv columns header i want to insert the data into respective columns in mysql table. For this, I need to get all the table fields name from Zend Framework Controller or Model.
I have tried with:
**
$metadata = new Zend\Db\Metadata\Metadata($adapter);
$table = $metadata->getTable($tableName);
$table->getColumns();
**
But, it shows the error:
Fatal error: Class 'Import\Model\Zend\Db\Metadata\Metadata' not found.
How can I get all the mysql table fields name using Zend Framework 2?
you have to use a backslash before zend
correct one :
$metadata = new \Zend\Db\Metadata\Metadata($adapter);
You can also use the use-statement at the top of your file.
namespace Import\Model;
use Zend;
Or you use the following:
namespace Import\Model;
use Zend\Db\Metadata\Metadata;
// ... lots of code here ;-)
$metadata = new Metadata($adapter);
At Header section
namespace Import\Model;
use Zend\Db\Metadata\Metadata;
In Modal function
$metadata = new Metadata($adapter);
$fields=$metadata->getColumnNames($table);
Instead of creating new instance of Metadata (its depricated), you should use the factory for this
use Zend\Db\Metadata\Source\Factory;
$metadata = Factory::createSourceFromAdapter($adapter);
Related
I have several fields in a form with dates and times ('dd-mm-yyyy', 'hh:mm').
When I want to save the form I use my own function to convert it for MySQL ('yyyy-mm-dd', 'H:i:s') via date() which I added to save() in Laravel Model Class.
But where is single place where I can put the same function to format output (SELECT queries)?
There is no single function in Laravel for data output. find(), findOrNew(), findOrFail(), ... - there are in different classes so I cant use single place to add the formatting function.
Is there any proper place to put formatting function?
You can use Laravel Date Mutator. Just put the following method in your Eloquent Model:
Suppose your have a table user in your database with expireDate column. then define this field in your model.
class User extends Eloquent {
protected $dates = ['expireDate'];
}
Just Replace FirstName with your database column name.
Then you can use laravel Carbon class to format your result.
$user = User::find(1);
return $user->expireDate->format('d-m-Y');
For more reference please read the http://laravel.com/docs/4.2/eloquent#date-mutators
Just add this to AppServiceProvider#register:
Carbon::setToStringFormat('d/m/y');
Now you can just echo $model->created_at and everything’s formatted correctly.
In App directory, create a new directory. Let call it "libraries",
then create a php file "CustomFunction.php" or whatever, this has to be a class as well, then put your function in this class.
So you have
class CustomFunction{
static function dateFormat(){
}
}
in App/libraries/
Then in App/start/global.php
Iside classloader on line 14, as below, add the path to your new folder:
ClassLoader::addDirectories(array(
app_path().'/libraries'
));
Then in your composer.json file, add:
"app/libraries" to "autoload" array.
Then in your view you can easily do this:
{{ CustomFunction::dateFormat() }}
I am new to yii and have a basic doubt.
I am working on an app where I have to store data submitted in one form into a new table.
As I know one table=one model
should I create a new model for the new table using gii?
Can't I just use the following code without creating the model in gii?
$modelA = new table_name;
$modelA->attributes = $_POST['table_name'];
$modelA->save();
No, you have to create model, because:
$modelA = new ModelClassName();
and not table name. You still may use relations for other tables, so you can have only one model:
$modelA->tableNameB->attributes = $_POST['attributes4B'];
I want to fill an empty model and save the model in a blob field for later use. My issue is i can not find how to add anouther row to the empty Model.
this works:
$test = LineItem::model();
$test->item_id = '2';
This does not work
$test->1->item_id = '3';
or
$test->item_id[1] = '3';
i have tried looking in the Yii documentation but i was unable to find an answer.
Thanks
Clarification
Im trying to create a false table using the model of a real table. I'm working on an invoicing system and i don't want to right the line items or invoice body information to the DB until it is "closed". Instead i want to fill the corresponding models that will then be serialized and stored in a BLOB field. Once the invoice is finished the data will be written to the table.
You should use
$test = new LineItem;
instead of
$test = LineItem::model();
for INSERT queries. And after setting properties
$test->save();
And so in every iteration.
For Yii framework, can we set attribute for save function using object results from find function like below?
$proposal = new Proposals;
$proposal_temp = Proposals_temp::model()->find('eid=?', array($users->eid));
$proposal->Attributes = $proposal_temp;
Would this actually save the Proposal_temp row into Proposals table?
No, you'll have to use the save() method. Attributes is just an array of columns in the table. You'll have to loop through the results of $proposal_temp and save each row. See Documentation
I'm trying to display data from a non-Sugar table in a custom listview in SugarCRM. Currently I'm running an SQL query in a custom view.list.php file, but this is displaying the data below the list rather than replacing the default query in the listview.
How can I replace the default query in a listview with custom SQL?
You don't have to go through all of that.
When you create a custom module in ModuleBuilder. Deploy the package
when edit the vardefs.php and Module_sugar.php and change the table_name to point to the new table. Then you don't actually have to write any special code and custom fields will work and complete the join for you.
class CustomModule_sugar extends SugarBean {
var $table_name = 'external_table';
I've managed to solve this by overriding the create_new_list_query() method in the module base class:
class CustomModule extends CustomModule_sugar {
function CustomModule(){
parent::CustomModule_sugar();
}
// this is the method which constructs the default SQL query
function create_new_list_query($order_by, $where, $filter, $params, $show_deleted, $join_type, $return_array, $parentbean, $singleSelect){
// call the parent method to populate all params - will cause errors/problems elsewhere otherwise
$ret_array = parent::create_new_list_query($order_by, $where,$filter,$params, $show_deleted,$join_type, $return_array,$parentbean, $singleSelect);
// override module sql with custom query
// alias external field names so they match the fields set up in Sugar
$ret_array['select'] = 'SELECT primary_id as id, date_added as date_entered, field_name as name, external_notes as notes';
$ret_array['from'] = ' FROM external_table';
// update these with appropriate SQL
$ret_array['where'] = '';
$ret_array['order_by'] = '';
return $ret_array;
}
}
This method creates an SQL statement which is used in /includes/ListView/ListViewData.php. I've aliased the field names selected from the external table to match the names of the fields set up in Sugar (easier than creating or renaming every single field).