PHPackages                             dandylion/advancedmedoo - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. dandylion/advancedmedoo

ActiveLibrary

dandylion/advancedmedoo
=======================

Adds extra features to Medoo

v1.2.1(2y ago)045MITPHPPHP &gt;=8.0.0

Since Sep 11Pushed 2y ago2 watchersCompare

[ Source](https://github.com/dandylion-tech/advanced-medoo)[ Packagist](https://packagist.org/packages/dandylion/advancedmedoo)[ Docs](https://serverbook.app)[ RSS](/packages/dandylion-advancedmedoo/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (16)Used By (0)

Advanced Medoo - Elevate Your Database Game with Medoo Extension
================================================================

[](#advanced-medoo---elevate-your-database-game-with-medoo-extension)

Welcome to Advanced Medoo, a powerful extension for [Medoo](https://medoo.in/) that takes your PHP database interactions to the next level. Say goodbye to repetitive database code and hello to a more efficient way of working with databases!

Features
--------

[](#features)

- **Custom Column Selection:** Advanced Medoo allows you to select columns in a more versatile way, making it easier to fetch the data you need.
- **Patch and Sync Functions:** Simplify data synchronization with patching and syncing functions, designed to streamline your workflow.
- **Column Similarity Search:** Easily determine how similar a specific column is to a given string with the "SIMILAR" function.

Installation
------------

[](#installation)

You can quickly install Advanced Medoo using Composer. This will also automatically install Medoo.

```
composer require dandylion/advancedmedoo

```

Usage
-----

[](#usage)

Getting started with Advanced Medoo is a breeze. Here's an example of how to use it:

```
use Dandylion\AdvancedMedoo;

// Initialize Advanced Medoo with your database configuration
$database = new AdvancedMedoo([
    'database_type' => 'mysql',
    'database_name' => 'your_database',
    'server' => 'localhost',
    'username' => 'your_username',
    'password' => 'your_password'
]);

```

### Select all columns from table

[](#select-all-columns-from-table)

```
$database->select(
    'user',
    '[>]post'=>'user_id'
    [
        'user.name'
        'post.*'
    ]
);
//[{
//    "name": "my_name",
//    "user_id": 1,
//    "text": "post_text"
//    "date": "01/01/1970"
//}]

/* You can also put text in brackets to prepend a string in front of all the columns incase of duplicate column names */

$database->select(
    'user',
    '[>]post'=>'user_id'
    [
        'user.name'
        'post.* (post_)'
    ]
);

//[{
//    "name": "my_name",
//    "post_user_id": 1,
//    "post_text": "post_text",
//    "post_date": "01/01/1970"
//}]

```

Patch method *(allows you to update or create a record without having to check if the record exists)*
-----------------------------------------------------------------------------------------------------

[](#patch-method-allows-you-to-update-or-create-a-record-without-having-to-check-if-the-record-exists)

```
//[
//    {
//        "user_id": 1,
//        "post_id": 1,
//        "text": "This is my first post",
//        "privacy": "public"
//    },
//    {
//        "user_id": 2,
//        "post_id": 2,
//        "text": "This is a different user post",
//        "privacy": "public"
//    }
//]
$database->patch(
    'posts',
    [
        [
            "user_id": 1,
            "post_id": 1,
            "text": "This is my first post",
            "privacy": "private"
        ],
        [
            "user_id": 1,
            "post_id": 3,
            "text": "This is my second post",
            "privacy": "public"
        ]
    ],
    [ // uses AND to check if record exists
        "user_id",
        "post_id"
    ]
);
//[
//    {
//        "user_id": 1,
//        "post_id": 1,
//        "text": "This is my first post",
//        "privacy": "private"
//    },
//    {
//        "user_id": 2,
//        "post_id": 2,
//        "text": "This is a different user post",
//        "privacy": "public"
//    },
//    {
//        "user_id": 1,
//        "post_id": 3,
//        "text": "This is my second post",
//        "privacy": "public"
//    }
//]

```

Sync method *(same as patch method except will also delete records not found)*
------------------------------------------------------------------------------

[](#sync-method-same-as-patch-method-except-will-also-delete-records-not-found)

```
//[
//    {
//        "user_id": 1,
//        "post_id": 1,
//        "text": "This is my first post",
//        "privacy": "public"
//    },
//    {
//        "user_id": 2,
//        "post_id": 2,
//        "text": "This is a different user post",
//        "privacy": "public"
//    }
//]
$database->patch(
    'posts',
    [
        [
            "user_id": 1,
            "post_id": 1,
            "text": "This is my first post",
            "privacy": "private"
        ],
        [
            "user_id": 1,
            "post_id": 3,
            "text": "This is my second post",
            "privacy": "public"
        ]
    ],
    [ // uses AND to check if record exists
        "user_id",
        "post_id"
    ]
);
//[
//    {
//        "user_id": 1,
//        "post_id": 1,
//        "text": "This is my first post",
//        "privacy": "private"
//    },
//    {
//        "user_id": 1,
//        "post_id": 3,
//        "text": "This is my second post",
//        "privacy": "public"
//    }
//]

```

SIMILAR stored procedure
------------------------

[](#similar-stored-procedure)

This stored procedure calculates how similar a column is to a given string.

> ***Please run this sql query on the database before using the sql function.***

```
DROP FUNCTION IF EXISTS SIMILAR;
DELIMITER //
CREATE FUNCTION SIMILAR(s1 VARCHAR(255), s2 VARCHAR(255))
RETURNS FLOAT
DETERMINISTIC
BEGIN
    DECLARE s1_len, s2_len, i, j, matches INT;
    DECLARE s3,checker VARCHAR(255);
    IF LENGTH(s1) > LENGTH(s2) THEN
    SET s3 = s2;
    SET s2 = s1;
    SET s1 = s3;
    END IF;
    SET s1_len = LENGTH(s1) - 2;
    SET s2_len = LENGTH(s2) - 2;
    SET i = 1;
    SET matches = 0;
    WHILE i
