Vtiger update field value from related module - php

I have a code on Save.php of Module Invoice:
public function saveRecord($request) {
...
$received = $request->get('received');
$balance = $request->get('balance');
$relProject = $request->get('relProject');
$project = Project_Record_Model::getInstanceById($relProject);
$fields = $project->getField('cf_938');
$fieldInstance = Vtiger_Field::getInstance($fields->id);
if ($fieldInstance) {
$fieldInstance->value = $received;
$fieldInstance->save();
}
}
Why save() is not work? When I save Invoice my field is not update on project.. I need fix it.

You should use the Record Model object to edit the record, not the Field Model. Try something like this:
public function saveRecord($request) {
...
$received = $request->get('received');
$balance = $request->get('balance');
$relProject = $request->get('relProject');
$project = Project_Record_Model::getInstanceById($relProject);
$project->set('mode', 'edit');
$project->set('cf_938', $received);
$project->save();
}

Related

Updating a row in Laravel 5.7 doesn't work, how do I fix it?

I'm trying to update a row in mysql table. However, when I click the register button, it doesn't do anything. I'm using Laravel.
Here's my update function:
public function update(Request $request)
{
$idpawn = $request['idprestamo'];
$paynumber = $request['numeropago'];
$payqty = $request['payqty'];
$statuspawns = statuspawns::find($idpawn,$paynumber);
$updateqty = $statuspawns->totalpayment - $payqty;
if($updateqty == "0"){
$status = "Pay";
}
else{
$status = "Partial Payment";
}
$statuspawns->total = $updateqty;
$statuspawns->status = $status;
$statuspawns->save();
return redirect()->back();
}
Thanks for your help.
I think the problem might be that you need square brackets instead of parenthesis since it's an array. $request['idprestamo']
$request['numeropago']
$request['payqty']
I'd like to also add that you can do it this way also with a magic method...
$request->numeropago
You have to access the Request this way $request->input_name not this way $request('input_name') because it is a Laravel Data Collection
In the end of the day, your function will look like this:
public function update(Request $request)
{
$idpawn = $request->idprestamo;
$paynumber = $request->numeropago;
$payqty = $request->payqty;
$statuspawns = statuspawns::find($idpawn,$paynumber);
$updateqty = $statuspawns->totalpayment - $payqty;
if($updateqty == "0"){
$status = "Pay";
}
else{
$status = "Partial Payment";
}
$statuspawns->total = $updateqty;
$statuspawns->status = $status;
$statuspawns->save();
return redirect()->back();
}
UPDATE
In Laravel 5.7 you have to use the update() method instead of the save() method to update a row.
You are not retrieving the record correctly. you have to pass only id to find(). As you have passed two parameters.
change this statement to..
$statuspawns = statuspawns::find($idpawn,$paynumber);
To
$statuspawns = statuspawns::find($idpawn);
Now you can update the columns which you want to update.

BadMethodCallException in Macroable.php line 81:Method update does not exist

I an developing a page to create, update, delete and view an event in which there is error while updating the event. There is a event table and a event_collection table. In that event_collection table there is event_id which is id of an event and a collection_id which is from other table collection.
When i create an event, all the data gets stored in event table except the collection one. in the collection table data gets stored in one by one manner like if I check 2 items in collection, it will generate 2 ids with same event_id and 2 collection_ids.
There is problem in update, when i try to update the code, it gives me error as
BadMethodCallException in Macroable.php line 81:
Method update does not exist.
Update method is:
public function update(EventRequest $request, $id)
{
$event = Event::findOrFail($id);
$input = $request->all();
$input['days_of_week'] = serialize(Input::get('days_of_week'));
$query = $event->update($input);
$checkbox_selection = Input::get('agree');
$choosen_checkbox = $id;
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
// return $collection_event;
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
// $collection_id = $id;
foreach($collection_event as $k){
// return $k;
if($k->event_id == $choosen_checkbox){
$data = $request->all();
$data['event_id']= $choosen_checkbox;
$data['collection_id'] = $collection;
$collection_event->update($data);
}
}
}
}
My store method is:
public function store(Request $request)
{
$checkbox = Input::get('days_of_week');
$checkbox_selection = Input::get('agree');
// return $checkbox_collection;
$input = $request->all();
$input['days_of_week'] = serialize($checkbox);
$query = Event::create($input);
$event_id = $query->id;
$pro_id = $query->provider_org_id;
/*For the checkbox selection, if they are multiple store each separately*/
if (!is_null($checkbox_selection)) {
foreach ($checkbox_selection as $collection) {
$collection_id = $query->id;
if($collection_id){
$data = $request->all();
$data['event_id']= $collection_id;
$data['collection_id'] = $collection;
$create_collection = EventCollection::create($data);
}
}
}
return view('event.pic_upload',compact('event_id','pro_id'));
}
Store method works properly! Can someone please tell any solution? I am badly stucked in this.
I do not think the 'update' method works on collections.
This line will return a collection
$collection_event = EventCollection::where('event_id',$choosen_checkbox)->get();
You do not want a collection, rather a query. As given in the docs:
`App\Flight::where('active', 1)
->where('destination', 'San Diego')
->update(['delayed' => 1]);`
Try removing the '->get()' from the statement.

Magento: Get record of my model by custom Field

Is it possible to get a record of my model by another field of my model?
The normal way
$model = Mage::getModel('foo_bar/baz');
$model->load($id);
// do something with the loaded record
But i need something like this
$model = Mage::getModel('foo_bar/baz');
$model->loadByAnotherFieldOfModel($value)
// do something with the loaded record
is that possible?
$model = Mage::getModel('foo_bar/baz');
$model->load('field_value', 'field_name');
use this
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'computer');
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'hp2312');
// Load by SKU
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'computer123');
Goto model file
NameSpace/Yourmodule/Model/YourModel.php
add below code
public function loadByField($fieldvalue)
{
$this->_getResource()->loadByField($this, $fieldvalue);
return $this;
}
AND
NameSpace/YourModule/Model/Resource/YourModel.php
and code is
public function loadByField(NameSpace_YourModule_Model_YourModel $Object, $fieldvalue)
{
$adapter = $this->_getReadAdapter();
$bind = array('fieldname' => $fieldvalue);
$select = $adapter->select()
->from($this->getMainTable(), 'tablePrimaryKey')
->where('fieldname = :fieldname');
$modelId = $adapter->fetchOne($select, $bind);
if ($modelId) {
$this->load($Object, $modelId );
} else {
$Object->setData(array());
}
return $this;
}

How to submit data to multiple tables in the same form

I have 2 tables: listings and listings_specifications
Listings table
id
type
status
location
specifications_id
Listings_specifications table
id
listing_id
swimming_pool
water_well
I need to select the specifications (checkboxes) on the same form with which I add a listing. I have created all the forms, views, models, controllers but I think I got some logic wrong.
Listing.php model
protected $table = 'listings';
public function contact()
{
return $this->BelongsTo('contacts');
}
public function specifications()
{
return $this->BelongsTo('listings_specifications');
}
Specification.php model
protected $table = 'listings_specifications';
public function listings()
{
return $this->BelongsTo('listings');
}
ListingsController.php (where the data gets saved in the database)
$listing = new Listing;
$contact = new Contact;
$listing->status = Input::get('status');
$listing->listingfor = Input::get('listingfor');
$listing->propertystatus = Input::get('propertystatus');
$listing->propertytype = Input::get('propertytype');
$listing->userid = Auth::user()->id;
$listing->location = Input::get('location');
$listing->contact_id = $contact_id;
$listing->save();
$specifications = Specification::find($id);
if( $listings->save() ) {
$specifications = new Specification;
$specifications->listing_id = $id;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$specifications->save();
}
I'm getting this error: Undefined variable: id
Where did I go wrong?
Thank you
It looks like you have some logic errors.
First of all, you are never setting $id anywhere, but that's okay because you really don't need it.
Remove the $specifications = Specification::find($id); line because that's not doing anything.
Then change your last section to something like this...
if( $listings->save() ) {
$specifications = new Specification;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$listing->specifications()->save($specifications);
}
$listing->specifications()->save($specifications); will automatically save the new specification with the correct listing_id for you.
Modify your Listing model's specifications relationship to...
public function specifications()
{
return $this->hasMany('Specification');
}
I'm assuming here one listing can have many specifications. If not, you can easily just change that to a hasOne.
You use $id in the line $specifications = Specification::find($id); but you don't define it before.

Update query updates all rows in database instead of particular row

I am new to Zend framework and I am trying to update data in database and grid, but instead of updating particular row all rows are getting updated. please help me with this.
Here is my controller code.
public function editAction()
{
$form = new Application_Form_user();
$this->view->form = $form;
if($this->getRequest()->isPost())
{
$formData= $this->getRequest()->getPost();
if($form->isvalid($formData))
{
$client= new Application_Model_DbTable_Client();
$firstname = $formData['firstname'];
$lastname = $formData['lastname'];
$email = $formData['email'];
$client->updateClient('Id',$firstname,$lastname,$email);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
else
{
$id=$this->getRequest()->getparam('id');
if($id>0)
{
$client= new Application_Model_DbTable_Client();
$clients = $client->getClient('Id');
$form->populate($clients[0]);
}
}
}
And here is my model code.
public function updateClient($id,$firstname,$lastname,$email)
{
$data=array('firstname'=>$firstname,
'lastname'=>$lastname,
'email'=>$email);
$this->update($data,"Id=$id");
}
$client->updateClient('Id',$firstname,$lastname,$email)
You're passing the literal string 'Id' as the $id parameter to updateClient. Inside updateClient, you build a where condition with "Id=$id". Your where condition becomes where Id=Id, which is true for every record, so every record is updated.
You need to pass a variable containing the actual ID of the record you want to update.
try to change your code, so it looks like this:
if($form->isvalid($formData))
{
$client= new Application_Model_DbTable_Client();
$firstname = $formData['firstname'];
$lastname = $formData['lastname'];
$email = $formData['email'];
$id=$this->getRequest()->getparam('id'); // define your id
$client->updateClient($id,$firstname,$lastname,$email); // here, change Id into proper $id
$this->_helper->redirector('index');
}

Categories