Code Monkey home page Code Monkey logo

Comments (2)

Athari avatar Athari commented on May 26, 2024

The where method returns a sequence of elements. If you need only one element, you should use first (if you're absolutely sure at least one element exists) or firstOrDefault (it returns default null if the searched sequence is empty). It also returns the whole row, not just the value, so you need to get meta_value. So your code becomes:

$usermetas = $wpdb->get_results("SELECT * FROM {$wpdb->usermeta}");
foreach ($users as $user) {
    $dateOfBirth = from($usermetas)
        ->firstOrDefault(function($um) {
            return $um['user_id'] === $user->ID && $um['meta_key'] === 'co_dateofbirth' ;});
    if ($dateOfBirth != null)
        echo "co_dateofbirth value is: {$dateOfBirth->meta_value}<br/>";
}

Or, if all users are guaranteed to have a date of birth:

$usermetas = $wpdb->get_results("SELECT * FROM {$wpdb->usermeta}");
foreach ($users as $user) {
    $dateOfBirth = from($usermetas)
        ->first(function($um) {
            return $um['user_id'] === $user->ID && $um['meta_key'] === 'co_dateofbirth' ;});
    echo "co_dateofbirth value is: {$dateOfBirth->meta_value}<br/>";
}

Note that your code is inefficient because it puts the whole UserMeta table into memory while you only need dates of birth. It would be more optimal modify your SQL query to get all your data at once and only it.

The following query would return only the data you actually need:

SELECT u.id, um.meta_value
FROM user AS u
  INNER JOIN usermeta AS um ON um.user_id = u.id
WHERE um.meta_key = 'co_dateofbirth'

In general, if a query is possible within SQL, you should perform it within SQL. You should only use YaLinqo (and any filtering and transformation using array functions in general) only in cases SQL is unavailable.

(I haven't tested any of the code above.)

from yalinqo.

wpplumber avatar wpplumber commented on May 26, 2024

The code seems returning wrong field which is the 1st on table, is it a problem on the first function conditions!
image

from yalinqo.

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.