Laravel - keeping a running tally of a column through a transformer - php

So I have a Contribution model. I have a controller that pulls in all the contributions and sends them to a transformer like so:
My Controller:
public function showContributions (Request $request, $memberId)
{
$perPage = $request->input('per_page', 15);
$contribution = parent::getRepo('Contribution');
$contributions = Cache::tags(['contributions'])->remember("contributions.$memberId.2", 60, function() use ($perPage, $memberId, $contribution){
return $contribution->where('vip_id', $memberId)->where('fund_id', 2)->paginate($perPage);
});
$transformedData = $this->fractal->paginatedCollection($contributions, new ContributionTransformer(), 'contributions');
return $this->sendResponse($transformedData['contributions'], $transformedData['meta']);
}
My transformer:
public function transform(Contribution $contribution)
{
setlocale(LC_MONETARY, 'en_US.UTF-8'); // Set so that money_format uses the dollar sign instead of USD. Consider moving to bootstrap
$report = $contribution->report;
$employer = $report->employer;
$employerHours = $contribution->employerHours;
$contributionLocal = $contribution->local->local_code ?? '';
$employerLocal = $employerHours->local->local_code ?? '';
$reciprocalLocal = $contributionLocal === $employerLocal ? '0000' : $employerLocal;
$response = [
'id' => $contribution->member_hours_id,
'report_number' => $contribution->report_number_id,
'employer_code' => $employer->employer_code,
'employer_name' => $employer->employer_name,
'worked_date' => $report->ending_worked_date,
'received_date' => $report->receipt_date,
'report_local' => $contributionLocal,
'reciprocal_local' => $reciprocalLocal,
'unit_type' => $contribution->unitType->code_description,
'units_worked' => $contribution->units_worked,
'credited_units' => $contribution->units_credited,
'rate' => $contribution->unit_multiplier,
'reciprocal_rate' => $employerHours->reciprocal_multiplier,
'calculated_amount' => money_format('%.2n', $contribution->calculated_amount),
'received_amount' => money_format('%.2n', $contribution->actual_amount),
'owed_amount' => money_format('%.2n', $contribution->owed_amount),
];
return $response;
}
One of the fields in the contributions table is sub_hours. What they want me to do is keep a running tally of said field. In each subsequent row return that tally as hours_to_date. So in first row sub_hours is 32 and in the second row it is 60. In the first row hours_to_date will be 32 but in the second row it will be 92 and the third row it will be 92 + sub_hours of row 3 etc. I can't seem to figure out how I should keep track of this running tally and allow the transformer access to it. Any help would be appreciated.

Can you create a property on the transformer class? I haven't used transformers but something like
class ContributionTransformer{
private $tally;
function __construct(){
$this->tally = 0;
}
public function transform(Contribution $contribution){
...
$this->tally += $contribution->sub_hours;
...
}

Related

Laravel: php match does not seem to be working

I'm working on a Laravel app. I've created an enum like this:
<?php
namespace Domain\Order\Enums;
use Domain\Order\Models\Order;
enum OrderStatuses : string
{
case New = 'new';
case Pending = 'pending';
case Canceled = 'canceled';
case Paid = 'paid';
case PaymentFailed = 'payment-failed';
public function createOrderStatus(Order $order) : OrderStatus
{
return match($this) {
OrderStatuses::Pending => new PendingOrderStatus($order),
OrderStatuses::Canceled => new CanceledOrderStatus($order),
OrderStatuses::Paid => new PaidOrderStatus($order),
OrderStatuses::PaymentFailed => new PaymentFailedOrderStatus($order),
default => new NewOrderStatus($order)
};
}
}
In my order model I've got the following attribute:
protected function status(): Attribute
{
return new Attribute(
get: fn(string $value) =>
OrderStatuses::from($value)->createOrderStatus($this),
);
}
which as you can see receives some data and returns an Order status.
Now, I've got the following piece of code:
$order = Order::find($orderID);
$newOrder = match ($order->status) {
OrderStatuses::New => (new NewToPaidTransition)->execute($order),
NewOrderStatus::class => (new NewToPaidTransition)->execute($order),
'new' => (new NewToPaidTransition)->execute($order),
default => null,
};
but the value of $newOrder is always null, meaning the status is not being matched to any of the elements. There should be one single element there: NewOrderStatus::class, I just added the others for debugging purposes.
If I inspect the value of $order->status while running the debugger I'm getting that it is of type Domain\Order\Enums\NewOrderStatus so why it is not being matched?
Thanks
It looks like you are testing for equality between an instance of a class and a string of the class name.
Try
$newOrder = match (get_class($order->status)) {
OrderStatuses::New => (new NewToPaidTransition)->execute($order),
NewOrderStatus::class => (new NewToPaidTransition)->execute($order),
'new' => (new NewToPaidTransition)->execute($order),
default => null,
};

Laravel and a While Loop

I'm new to Laravel and at the moment I have a piece of code in a Controller which without the while loop it works, it retrieves my query from the database.
public function dash($id, Request $request) {
$user = JWTAuth::parseToken()->authenticate();
$postdata = $request->except('token');
$q = DB::select('SELECT * FROM maps WHERE user_id = :id', ['id' => $id]);
if($q->num_rows > 0){
$check = true;
$maps = array();
while($row = mysqli_fetch_array($q)) {
$product = array(
'auth' => 1,
'id' => $row['id'],
'url' => $row['url'],
'locationData' => json_decode($row['locationData']),
'userData' => json_decode($row['userData']),
'visible' => $row['visible'],
'thedate' => $row['thedate']
);
array_push($maps, $product);
}
} else {
$check = false;
}
return response()->json($maps);
}
I am trying to loop through the returned data from $q and use json_decode on 2 key/val pairs but I can't even get this done right.
Don't use mysqli to iterate over the results (Laravel doesn't use mysqli). Results coming back from Laravel's query builder are Traversable, so you can simply use a foreach loop:
$q = DB::select('...');
foreach($q as $row) {
// ...
}
Each $row is going to be an object and not an array:
$product = array(
'auth' => 1,
'id' => $row->id,
'url' => $row->url,
'locationData' => json_decode($row->locationData),
'userData' => json_decode($row->userData),
'visible' => $row->visible,
'thedate' => $row->thedate
);
You're not using $postdata in that function so remove it.
Do not use mysqli in Laravel. Use models and/or the DB query functionality built in.
You're passing the wrong thing to mysqli_fetch_array. It's always returning a non-false value and that's why the loop never ends.
Why are you looping over the row data? Just return the query results-- they're already an array. If you want things like 'locationData' and 'userData' to be decoded JSON then use a model with methods to do this stuff for you. Remember, with MVC you should always put anything data related into models.
So a better way to do this is with Laravel models and relationships:
// put this with the rest of your models
// User.php
class User extends Model
{
function maps ()
{
return $this->hasMany ('App\Map');
}
}
// Maps.php
class Map extends Model
{
// you're not using this right now, but in case your view needs to get
// this stuff you can use these functions
function getLocationData ()
{
return json_decode ($this->locationData);
}
function getUserData ()
{
return json_decode ($this->userData);
}
}
// now in your controller:
public function dash ($id, Request $request) {
// $user should now be an instance of the User model
$user = JWTAuth::parseToken()->authenticate();
// don't use raw SQL if at all possible
//$q = DB::select('SELECT * FROM maps WHERE user_id = :id', ['id' => $id]);
// notice that User has a relationship to Maps defined!
// and it's a has-many relationship so maps() returns an array
// of Map models
$maps = $user->maps ();
return response()->json($maps);
}
You can loop over $q using a foreach:
foreach ($q as $row) {
// Do work here
}
See the Laravel docs for more information.

Unable to save select box options in Laravel 5.1

I am working on my first project using Laravel 5.1. Uses a selectbox in a form.
{!!Form::select('animal_parent[]', array('1' => 'opt1', '2' => 'opt2', '3' => 'opt3', '4' => 'opt4',), null, ['id' => 'animal_parent', 'disabled' => 'disabled', 'multiple' => 'multiple', 'class' => 'form-control'])!!}
Selection limited to two options which need to saved in two columns, male_parent and female_ parent of the animal table.
There are no male_parent and female_ parent element names in the form. Similarly no animal_parent field in animal table.
Values are set as expected in the code given below. However, the insert command does not reflect the newly set values and throws an error.
"ErrorException in helpers.php line 671: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array."
Any help would be much appreciated.
First attempt using mutators
public function setMaleParentAttribute()
{
$parent = Input::get('animal_parent');
$this->attributes['male_parent'] = intval($parent[0]);
}
public function setFemaleParentAttribute(AddAnimalRequest $request)
{
$parent = Input::get('animal_parent);
if (isset($parent[1])) {
$this->attributes['female_parent'] = intval($parent[1]);
} else {
$this->attributes['female_parent'] = intval($parent[0]);
}
unset($request->animal_parent);
}
Second attempt using the store() method in the controller.
$animal = new Animal($request->all());
$parent = Input::get('animal_parent');
$animal['male_parent'] = intval($parent[0]);
if (isset($parent[1])) {
$animal['female_parent'] = intval($parent[1]);
} else {
$animal['female_parent'] = intval($parent[0]);
}
unset($request->animal_parent);
Auth::user()->animals()->save($animal);
return redirect('animals');
The problem was then solved with a change in UI. I feel the problem could have been solved using the below method. Hope that helps someone.
$input = $request->all();
$parent = $input['animal_parent'];
$input['male_parent'] = intval($parent[0]);
if (isset($parent[1])) {
$input['female_parent'] = intval($parent[1]);
} else {
$input['female_parent'] = intval($parent[0]);
}
unset($input['animal_parent']);
$animal = new Animal($input);
$animal->save();`

Custom Widget doesn't display the same amount of choices

I try to create a customWidget with a special tablemethod to only display the pre selected choices of the user, this is the form :
$this->widgetSchema['Books_list'] = new MyWidgetFormThematicSelector(array(
'multiple' => true,
'model' => 'Books',
'table_method' => array('method' => 'getOnlySelected', 'parameters' => array($this->getObject()->getId())),
'expanded' => true,
));
this is the method getOnlySelected:
$q = Doctrine::getTable('BooksAuthors')
->createQuery('ba')
->select('ba.position,ba.name')
->leftJoin('ba.Books b')
->where('ba.BooksAuthors_id = ?', $id);
echo count($q); //return 4
return $q;
this method return 4 elements which is normal then if i try to echo the values of the getChoices method from the widget I get only 1 in return !?
class MyWidgetFormThematicSelector extends sfWidgetFormDoctrineChoiceWithParams {
public function configure($options = array(), $attributes = array())
{
parent::configure($options, $attributes);
}
public function getChoices() {
$choices = parent::getChoices();
echo count($choices); // return 1
return $choices;
}
public function render($name, $value = null, $attributes = array(), $errors = array()) {
return parent::render($name, $value, $attributes, $errors);
}
}
What's going on here ?
I create a similar widget in the same form where the probleme does not occurs, and it s quite the same code...
thx
I solve this problem by setting the attribute 'key_method' => 'myUniqueId', in the form where the widget is called...
Cause Ive got two primary keys in my table and the sfWidgetFormDoctrineChoiceWithParams widget use the one which was identic for all the results as the key for the array choices, so the size of the array was always one...By setting the other primary key as the main key of the getChoices method I get the correct result.

Codeigniter Unit-testing models

I'm new to unit-testing, so this is maybe a little dumb question.
Imagine, we have a simple model method.
public function get_all_users($uid = false, $params = array()){
$users = array();
if(empty($uid) && empty($params)){return $users;}
$this->db->from('users u');
if($uid){
$this->db->where('u.id',(int)$id);
}
if(!empty($params)){
if(isset($params['is_active']){
$this->db->where('u.status ', 'active');
}
if(isset($params['something_else']){ // some more filter actions}
}
$q = $this->db->get();
if($q->num_rows()){
foreach($q->result_array() as $user){
$users[$user['id']] = $user;
}
}
$q->free_result();
return $users;
}
The question is how a _good test would be written for it?
UPD: I guess, the best unit-testing library for CI is Toast, so example i'm looking for, preferable be written using it.
Thanks.
I'm using toast too, and mostly I use it to test a model methods. To do it, first truncate all table values, insert a predefined value, then get it. This is the example of test I used in my application:
class Jobads_tests extends Toast
{
function Jobads_tests()
{
parent::Toast(__FILE__);
// Load any models, libraries etc. you need here
$this->load->model('jobads_draft_model');
$this->load->model('jobads_model');
}
/**
* OPTIONAL; Anything in this function will be run before each test
* Good for doing cleanup: resetting sessions, renewing objects, etc.
*/
function _pre()
{
$this->adodb->Execute("TRUNCATE TABLE `jobads_draft`");
}
/**
* OPTIONAL; Anything in this function will be run after each test
* I use it for setting $this->message = $this->My_model->getError();
*/
function _post()
{
$this->message = $this->jobads_draft_model->display_errors(' ', '<br/>');
$this->message .= $this->jobads_model->display_errors(' ', '<br/>');
}
/* TESTS BELOW */
function test_insert_to_draft()
{
//default data
$user_id = 1;
//test insert
$data = array(
'user_id' => $user_id,
'country' => 'ID',
'contract_start_date' => strtotime("+1 day"),
'contract_end_date' => strtotime("+1 week"),
'last_update' => time()
);
$jobads_draft_id = $this->jobads_draft_model->insert_data($data);
$this->_assert_equals($jobads_draft_id, 1);
//test update
$data = array(
'jobs_detail' => 'jobs_detail',
'last_update' => time()
);
$update_result = $this->jobads_draft_model->update_data($jobads_draft_id, $data);
$this->_assert_true($update_result);
//test insert_from_draft
$payment_data = array(
'activation_date' => date('Y-m-d', strtotime("+1 day")),
'duration_amount' => '3',
'duration_unit' => 'weeks',
'payment_status' => 'paid',
'total_charge' => 123.45
);
$insert_result = $this->jobads_model->insert_from_draft($jobads_draft_id, $payment_data);
$this->_assert_true($insert_result);
//draft now must be empty
$this->_assert_false($this->jobads_draft_model->get_current_jobads_draft($user_id));
}
}
I'm using AdoDB in my application, but don't get confuse with that. You can do $this->db inside the test controller, after you load the database library. You can put it in autoload so it will automatically loaded.
See that in my code, before the test is run, the table is truncated. After run, I will get any error that might occured. I do assert for a predefined insert and update. Using Toast to test the model will make you sure that the model's method doing exactly the task that you want it to do. Make the test that you need, and make sure you cover all the possibilities of input and output values.

Categories