Download the PHP package nantaburi/mongodb-sql-model without Composer
On this page you can find all versions of the php package nantaburi/mongodb-sql-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mongodb-sql-model
mongodb-sql-style
Installing ` Mongodb using SQL style
- Configuraton add setting in config/database.php of laravel
- Authorization Database default as admin if no value [options][authen_db] `
Run Nantabury/Mongodb standalone without laravel :
- if you don't have laravel wasn't installed you have only base on composer package go to create file
.env
under project's directory and then copy all below to .env
`
- Using Laravel for SQL query style example below
- Create Model - using command
php artisan make:model UserDbModel
at laravel root project and insertuse Nantaburi\Mongodb\MongoNativeDriver\Model
on top- ex-fillable used to be
protected $fillable = ["useid","username","lastname","password"]
will replace with $schema as example below - Example :
protected $schema [ "userscollection" , ["useid","username","lastname","password"] ]
- Magic create index with two ways
- first way : add array with key [ Index => true ] if you want to add option Unique just add more key [ 'Index' => true , 'Unique' => true ]
- secound way : do multiple keys with create key
"$__MULTIPLE_INDEX_01"
on same level of collection change at the end_01
to be other once you have more one multiple keys you can use_02
even_AB
support as well
- Magic create collection for counters auto increasement number by add
'AutoInc' => true ,
and also have option'AutoInc' => true , 'AutoIncStartwith' => 10,
default datatype as double max number can be 2^1023 - Magic creation of Index and Magic counter will auto create affective once you run first insert or you can do [NameModel]::InitIndexAutoInc()
- Example : run at Laravel controller just first time or do change schema
UserModel::InitIndexAutoInc()
- Example : run at Laravel controller just first time or do change schema
- ex-fillable used to be
- to protect update option add
'UpdateProtected'=>true
once you inserted and do you want to protect update such as price , ID Card number , you can put option exampleidcard => [ 'UpdateProtected'=>true ]
into option of that field
- Create Model - using command
`
-
Example get created magic index and the counters of each collection after run insert
-
Magic create counter collection run command with mongo shell
db.services.getIndexes()
` -
Head functions
Functional Head function Example set database [DBModel]::collection(string $collectionName)
[DBModel]::collection('Products')->
- Middle functions
Functional Middle function Example display documents ->select( $field1 , $field3 ....)->
->select('*')->
->select('userid' , 'username' , 'password')->
->select('userid as uid' , 'username as uname' , 'password as pass')->
->select('users.userid as uid' , 'users.username as uname' , 'users.password as pass'->)->
where ->where( String $field ,String $Operator, $values )->
->where("userid" ,"=", 100 )->
where in ->wherein(string $filed , array $values )->
->where(string $filed ,"in",array $values )->
->wherein("userid" , [1,4,5,6,10])->
->where("userid" ,"in" ,[1,4,5,6,10])->
where or where ->orwhere(String $field ,String $Operator, $values )->
->where("userid",'=',1)
->orwhere("userid",'>=',10)->
where and where ->andwhere(String $field ,String $Operator, $values )->
->where("userid",'=',1)
->andwhere("username",'=','supachai')->
join collection ->join(String $foreignCollection ,String $localCollection["."]localKey,String $foreign["."]foreignkey)->
->join("products_group","products.typeid","product_group.typeid" )->
leftjoin collection ->leftjoin(String $foreignCollection ,String $localCollection["."]localKey,String $foreign["."]foreignkey)->
->leftjoin("products_group","products.typeid","product_group.typeid" )->
Group by ->groupby(String $field1 ,String $field1...)->
#group same with ->select() function shortcut with '$selected'->groupby( string '$selected')->
->groupby("userid","username")->
->groupby('$selected')->
sort order by ->orderby(String $field1 ,String $field1...)->
->orderby("userid","username")->
Limit display output ->limit(int $limitPerpage [,Int $offset])->
->limit(10)->
->limit(10,40)->
- End functions
Functional End function Example get all documents static functon:: DBModel::all( null )
DBModel::all()
get documents ->get(null)
->get()
getgroup documents
to group request join two collections->getgroup(null)
-->getgroup( ) ;
get one first document ->first(null)
->first()
random documents
*request function groupby()
->random(int $numberDoc)
->select('id','name')->where("sale" , "=" , 1 )->groupby('$seleted')->random(2)
Pagination ->paginate(int $perpage)
->paginate(10)
Update document # request where match where( string $key ,string $operator ,v $value )->update(array $DictArray = null )
->where("userid" , "=" , 1 ,)->update(["password" => "AAA321" ])
->where('id',"=",454)
->andupdate( [ "name" => "PHONE UPDATE 11.0 v3" , 'price' => 25200 ])
->andupdate("description","=","Iphone version Thai v3")
->update("description_th","New iPhone 11.0 v3th ") ;
Delete document ->delete(string $key ,string $operator ,string $value )
->delete(["userid", "=", 1 )
->where("id" ,"=",444)
->andwhere("id" ,"=",442)
->delete("id",">",440);
Insert document ->insert(array $DictArray)
->insert(["userid" => 1 , "username" => "Supachai" , "password" => "321PASS" ])
Insert document and get ID ->insertGetId(array $DictArray [,string $fieldGet] )
$response = CompanyUser::collection("users")->insertGetId(["userid" => 1 , "username" => "Supachai" , "password" => "321PASS" ])
->insertGetId(["userid" => 1 , "username" => "Supachai" , "password" => "321PASS" ] , 'username' )
->insertGetId(["userid" => 1 , "username" => "Keattisak" , "password" => "PhomPoung" ] , 'userid' )
-
-
Where comparison operation
- to comparison macthing field to use function format ->where( $field , $Operator , $value) for $Operator list see below table
Operator description Example "=" operator matches documents where the value of a field equals the specified value
->where("userid","=",100)
">" selects those documents where the value of the field is greater than (i.e. >) the specified value
->where("userid",">",5)
">=" selects the documents where the value of the field is greater than or equal to (i.e. >=) a specified value (e.g. value.)
->where("userid",">=",5)
"in" The "in" operator selects the documents where the value of a field equals any value in the specified array. To specify an "in" expression
->where("userid","in",[5,11,10])
"nin" The "nin" operator selects the documents where the value of a field equals any value not in the specified array. To specify an "nin" expression
->where("userid","nin",[5,11,10])
"<" < selects the documents where the value of the field is less than (i.e. <) the specified value.
->where("userid","<",5
"<=" <= selects the documents where the value of the field is less than or equal to (i.e. <=) the specified value.
->where("userid","<=",10)
"!=" != selects the documents where the value of the field is not equal to the specified value. This includes documents that do not contain the field.
->where("username","!=","Supachai")
"like" != selects the documents where the value of the field is like machting values.
->where("username","like","Supa%")
->where("username","like","%chai")
->where("username","like","%upa%")
- spacial command on function select()
Operator description Example count(*) count all documents to be number
->select('count(*)')->get()
- spacial functions
class function() description Example [ClassName]::getSequence([Auto Increase field],[Collection]) to auto increase number
ModelDB::collection("customers")->insert(["id" => ModelDB::getSequence('id','customers') , "description" => "Elon mask"]) ;
- Create Laravel controller
- using command
php artisan make:controller --model=UserDbModel
at laravel root project - then edit and insert basic SQL example :
select * from user where username like 'suphacha%' and age > 18 or mooban = 'Pangpoi' ;
- using SQL transform to mongodb showing below : ` Changing database and collection if you have permission of user and password on the same multiple databases you can change database difference in file ./config/database.php
- examples change just database DataBaseModel::database("shopping") .... , change database and collection DataBaseModel::database("shopping","products") ...
- using command
`
- switch collection don't need to re-create new other Model file
- put begin with ->collection('[Collection Name]') see example below
`
- switch collection don't need to re-create new other Model file
- put begin with ->collection('[Collection Name]') see example below
- and many way to get data
`
- Controller
- join collectios code example below
- once you use groupby() request select() all of fields same fields in groupby() if seleted fields are not be field's member in groupby() output will display with empty data on that selected field
- if you want to use shortcut of
->groupby( )
to be same as all fields in->select("field1 as fl1","field2")
you can do shortcut by add one operator'$selected'
into->groupby('$selected')
also auto removeas
operator `
-
Getgroup
- Function to support side menu or dropdown menu
- request join two table ` On Laravel blade views file test.blode.php do getgroup() same as below :
`
- Paginate
- pagination are displays data and page breakedown with data perge
- paginate going to don't care function limit() if you have added into order command
- Output of data will is in property ->items it also show below
- Output of pages number linkdata as ->link and drawing of html code by use method ->link()
- switch to page 2 using as example : http://127.0.0.1/?page=2
`
- insert via Controller
- insert prepare code example below
- $fillable had removed replace with $schema and fillable will run behind $schema
- once collectaion and field data isn't in schema member insert will reject and has error
- find below example to use function
getModifySequence()
for auto increase number was bild-in this function you have to create schema to prepare on Model file then set'AutoInc' => true , 'AutoIncStartwith' => 101,
with
`
-
Update
- to prevent update field put in schema
idcard => [ 'UpdateProtected'=>true ]
- update can be many style do you need to see examples below `
- to prevent update field put in schema
-
Delete documents
- request begin with where statement
`
- Handle insert error in view
- add script below into your view file.blade.php
`