zf2 DB insert using custom query is not working - insert

We are developing a website using ZF2. We alreday started the project. We installed ZF2 skelton application and integrated the album module. Everything seems working. Now we are trying to integrate multiple zend forms in a module. Forms eveyrything we made [ by modifying the albulm module elements) I am able to insert/update/delete etc data in to the default table that set on the module.php file [this is for form 1].
I have another form which requires to insert/delete/update to another table, I created new form class, Table class (on model) etc. When I tried to insert the data in to Db using custom query format, it throws error:
File:
/home/catholic/public_html/propertydosth/vendor/zendframework/zendframework/library/Zend/Db/Sql/Insert.php:298
Message:
The key adapter was not found in this objects column list.
It seems adaptor value is not getting something..
My Code for insert query:
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$insert = $sql->insert('dad_cms');
$newData = array(
'category_id' => $admin->category_id,
'cms_title' => $admin->cms_title,
'cmd_desc' => $admin->cmd_desc,
'seo_keywords' => $admin->seo_keywords,
'seo_titles' => $admin->seo_titles
);
$insert->values($newData);
$selectString = $sql->getSqlStringForSqlObject($insert);
$results = $insert->adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
I am getting correct query value when I echoed $selectString .
Any one please suggest what is wrong in my code to get it done.

Finally Sam helped me on this issue:
So correct Answer become [this will be helpful somebody having similar issue. Even I spent lot of time to debug the same]:
$adapter = $this->tableGateway->getAdapter();
$sql = new Sql($adapter);
$insert = $sql->insert('dad_cms');
$newData = array(
'category_id' => $admin->category_id,
'cms_title' => $admin->cms_title,
'cmd_desc' => $admin->cmd_desc,
'seo_keywords' => $admin->seo_keywords,
'seo_titles' => $admin->seo_titles
);
$insert->values($newData);
$selectString = $sql->getSqlStringForSqlObject($insert);
$results = $adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);

Related

Passing Stripe Payment Methods to Laravel View

I am trying to load previously used cards using the Stripe API using Laravel using this link from Stripe https://stripe.com/docs/api/payment_methods/list?lang=php
Here is the snippet of code from my controller:
$stripe = new \Stripe\StripeClient(
'sk_test_51GueZuLq4MEy
);
$customer_id = "cus_HhnBT9fpjxW3hn";
$paymentMethods = $stripe->paymentMethods->all([
'customer' => $customer_id,
'type' => 'card',
]);
$pm = ($paymentMethods->data);
return view('payment.details', $pm);
However when I am trying to pass the card data into my view I am unable to do so. The variable I am passing in views is:
{{ $pm }}
The error message I get is that my variable is not recognised. The data I am trying to access is $paymentmethods->-data->card->last4
Any help is always appreciated
$stripe->paymentMethods->all returns an object with a data attribute, where the data is an array of PaymentMethods, in this case, you'll likely want to access the first element of data.
Try updating to:
$pm = $paymentMethods->data[0];

how to add table in phpword with laravel

i am trying to draw a table in phpword with laravel
code below is example of writing something in the doc file.
$wordTest = new \PhpOffice\PhpWord\PhpWord();
$newSection = $wordTest->addSection();
$desc1 = "The Portfolio details is a very useful feature of the web page. You can establish your archived details and the works to the entire web community. It was outlined to bring in extra clients, get you selected based on this details.";
$newSection->addText($desc1, array('name' => 'Tahoma', 'size' => 15, 'color' => 'red'));
$objectWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordTest, 'Word2007');
$objectWriter->save(storage_path('TestWordFile.docx'));
please give me an example of how i can draw table there
to draw a table i normally use this code
$table = array('borderColor'=>'black', 'borderSize'=> 1, 'cellMargin'=>50, 'valign'=>'center');
$phpWord->addTableStyle('table', $table);
$table = $section->addTable('table');
$table->addRow();
$table->addCell(1750)->addText(htmlspecialchars("#"),$bold);
$table->addCell(1750)->addText(htmlspecialchars("Attendance Date "),$bold);

should i put this code in the model or controller in laravel

ok so I am upping my Laravel and MVC skills..currently all this in the controller (i know its bad thats why I am here I am trying to do it better :D)
Receive Data from UI as post (Done)
I have to store a file (Done)
store that file ID and path in a Files Table (Done)
Create new record and use the above record ID using query builder(Done)
but should I keep this in the controller or does this belong in the model specificlaly getting the files and storing the record etc.
do i build it the long way in the controller and use save()?
or return from a new model for files and pass that through on return (this i feel is best way ).
FileModel (To be built)
saves files returns file ID from file table
Business Model saves buisness data
controllerfile:
use /app/fileModel
$fileStored = call file model and passes request->file
for storing
$businessFile = $fileStored;
Save()
I think I have answered my own question but I am just making sure.
I am known for coding in anti patterns so I am asking for a bit of guidance not code thank you
Also if anyone knows of a place to just discuss laravel stuff so not to put here I would love a a good conversation, some my questions may seem stupid, I just need a soundboard at times.
ok so this works and is my solution.
public function create(Request $request)
{
//get files from request
$Match1 = $request->file('Match1')->store('media/scores');
$Match2 = $request->file('Match2')->store('media/scores');
$Match3 = $request->file('Match3')->store('media/scores');
//build collection array to loop through saving them
$collection = collect([$Match1,$Match2,$Match3]);
// store return paths in to new array
$mediapaths = array();
//loop through collections saving details in to files table pushing to the paths array
foreach($collection as $MatchScore) {
$ScoresSaved = Files::create([
'user_id' => $request->userID,
'file_name' => $request->title,
'type' => 'png',
'category_id'=>3,
'path' => $MatchScore,
'is_public'=>0
]);
array_push($mediapaths, $ScoresSaved);
}
//Save the score with new media path to the model
$scores = new Scores;
$scores->tournament=22;
$scores->round='Finals';
$scores->homeArcade=2578;
$scores->homePlayer=2;
$scores->opponenet=2;
$scores->Match1=$mediapaths[0]['id'];
$scores->Match2=$mediapaths[1]['id'];
$scores->Match3=$mediapaths[2]['id'];
$scores->Match1Score=22;
$scores->Match2Score=44;
$scores->Match3Score=88;
$scores->comments='testing full flow';
$scores->winner=2;
$scores->referee=3;
$scores->confirmed=1;
$scores->dispute=0;
$scores->submitedBy=2;
$scores->save();
return response()->json([
'result' => 'Success'
]);
}

CakePHP 3.x unique validation not working for saving multiple records

I have a Questions table which has a validation like :
$validator
->notEmpty('title')
->add('title', [
'unique' => [
'rule' => [
'validateUnique',
['scope' => ['subject_id', 'chapter_id']]
],
'provider' => 'table'
]
]);
I want to save following records into my table at a time.
Array
(
[0] => Array
(
[subject_id] => 1
[chapter_id] => 4
[title] => What is a .ctp file used for in CakePHP?
)
[1] => Array
(
[subject_id] => 1
[chapter_id] => 4
[title] => What is a .ctp file used for in CakePHP?
)
)
I try to save it using saveMany() method. It save both records i.e. validation not working. I also try following code for transactional() method instead of saveMany() method, but validation also not working.
$entities = $this->Questions->newEntities($records);
$this->Questions->connection()->transactional(function () use ($entities) {
foreach ($entities as $entity) {
$this->Questions->save($entity);
}
});
My validation works fine if I save the records one by one using save() method or my records already saved in database. Why my unique validation not working for saveMany() and also for transactional() for duplicate new entities?
Validation happens before saving
Validation happens before saving, so that behavior is to be expected, given that the rule looks up the database, and not the request data (it would only have access to one set of data at the time anyways), ie, no matter how many datasets are being tested, none of the submitted ones will be saved yet, and therefore validation will pass unless a matching record already exists in the database.
So either create/patch and save all entities one by one in a custom transaction (and don't forget to add some proper failure checks),
$this->Questions->connection()->transactional(function () {
foreach ($this->request->data() as $set) {
$entity = $this->Questions->newEntity($set); // < validaton is being applied there
if (!$this->Questions->save($entity)) { // < not there
return false;
}
}
return true;
});
or use application rules instead.
Application rules are being applied in the saving process
Application rules are being applied in the actual saving process, ie upon calling Table::save(), so to avoid the hassle of using custom transactions, and generally to have a last line of defense, use them instead of/additionally to validation.
// QuestionsTable class
public function buildRules(\Cake\ORM\RulesChecker $rules)
{
$rules->add($rules->isUnique(['title', 'subject_id', 'chapter_id']));
// ...
return $rules;
}
See also
Cookbook > Database Access & ORM > Database Basics > Using Transactions
Cookbook > Database Access & ORM > Validation > Applying Application Rules
Cookbook > Database Access & ORM > Validation > Creating Unique Field Rules
Cookbook > Database Access & ORM > Validation > Validation vs. Application Rules

Codeigniter Datamapper not running get rules on fields with NULL values

I'm using Datamapper v1.8.2 with Codeigniter v2.1.2 and have a "get" rule that doesn't seem to run on fields with NULL values. Here's the model:
class Page extends Datamapper {
public $validation = array(
'name' => array(
'rules' => array('required'),
'get_rules' => array('get_page_name')
)
);
function _get_page_name($field)
{
$this->$field = 'TESTING '.$this->id;
}
}
Example code:
$page = new Page();
foreach ($page->get() as $p) echo $p->name;
When the table field name has any non-null value including an empty string it works fine outputting something like TESTING 358, but when the value is NULL (which is the default value for this field), it outputs nothing. There is no difference using get_iterated().
I guess I could work around this by changing the default value, but I'm wondering if I'm doing something wrong or missed something in the documentation, or maybe it's a bug? Does anyone know what the issue is?
Also, if someone could point me to the proper thread in the CI forums for Datamapper 1.8.2 support that would be great, I'm trying to find it and getting lost in a maze of links to threads for old versions of DM.
You need to add the allow_null to the get_rules array to make this work. I'm not sure about the intent of the creator but this is how get_rules are implemented (however i don't see it mentioned in the docs).

Resources