Giter VIP home page Giter VIP logo

Comments (4)

msarca avatar msarca commented on June 16, 2024

A simple $size = $db->from('vw_escrituracao_fiscal')->count(); will fixit for you.

Anyway, this is not a bug, the behaviour is correct. You should not reuse the same object instance for doing different SQL operations. Please refer to the PHP documentation regarding objects and references.

from database.

elvispdosreis avatar elvispdosreis commented on June 16, 2024

I'm sorry but I disagree with creating another instance to count the records only.
This example I went through is simple, in a more complex situation, where I am using multiple where and inner joins this would make a code replication just for me to count the records to do a pagination.

<?php
 $result = $db->from('vw_escrituracao_fiscal');

 $result->where('date')->between('2019-11-10', '2019-11-17');

//various conditions where
$result->where ....
$result->where ...
$result->where ...
$result->where ...

 $itens = $result->limit($params->limit)
            ->offset($params->offset)
            ->orderBy('id', $params->order_by ?? 'asc')
            ->select()
            ->fetchClass(ListNFSE::class)
            ->all();
// $size = $result->count();

you suggest i do that

<?php
 $result = $db->from('vw_escrituracao_fiscal');

 $result->where('date')->between('2019-11-10', '2019-11-17');

various conditions where
$result->where ....
$result->where ...
$result->where ...
$result->where ...

$size = $result->count();

from database.

msarca avatar msarca commented on June 16, 2024

You are not required to replicate your code, but you must use a different object instance for each operation. Something like bellow:

// Filter
$query = $db->from('my_database')
            ->where('column_a')->is($someValue)
            ->where('column_b')->is($otherValue);

// obtain new object instance
$counter = clone $query;

// Sort, paginate and fetch
$results = $query->limit($limit)
                ->offset($offset)
                ->orderBy('column_c')
                ->select()
                ->all();

// Count
$count = $counter->count();

Of course, it is not necessary to store the cloned instance into a variable. You could use it directly

// obtain new object instance & count
$count = (clone $query)->count();

from database.

elvispdosreis avatar elvispdosreis commented on June 16, 2024

thanks

from database.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.