Comments (20)
Let me know if this is the right path:
I changed the connect method to return the capsule, then I can do $capsule->getConnection()->setTablePrefix('new_prefix_here')
Ill try it out in the meantime and let you know...
from corcel.
Hi @AlexECP. If you're using Laravel you does not have to call connect
method. It's a very good suggestion to return Capsule
instance in connect
method.
from corcel.
Done! ;-) Commit: 5b5798f
from corcel.
@AlexECP just an advice. The change was made in dev
branch.
from corcel.
awesome. For my specific use case I need to connect even though Im on Laravel, since the wordpress DB is external.
Using getConnection()->setTablePrefix('new_prefix_here')
worked for me for our multi-site installation... possible someone else might want that advice...
anyways Thanks!
from corcel.
Welcome @AlexECP. If you're using Corcel with Laravel you can create a new connection on Laravel config files and set it to your Wordpress models, like Post
, for example. Just set protected $connection = 'wordpress'
and that's it. As Eloquent is already loaded by Laravel, Corcel only have to call it using the connection you've provided. Welcome!
from corcel.
ah I see, then I would just do something like $post->getConnection()->setTablePrefix(...) ? Right?
Thanks
from corcel.
Yes and no @AlexECP hehe. It's better to you set the table prefix in the connection configuration array (config folder) so every model you have $connection = 'wordpress'
for example you're going to use the table prefix you've just set. Easy ;-)
from corcel.
right, but for my case where its a multi-site, I will need to be changing the prefix all the time for example to publish a new post to 200 sites... before each save I will need to alter the prefix, I think
from corcel.
Hummm ok ;-) Maybe in the future we can think in a generic solution to this using Corcel :-)
from corcel.
Can't seem to find any documentation concerning corcel/corcel communicating with a multisite Wordpress installation - has nothing been written yet for this? Currently I have for example site_posts
for my initial Wordpress node, and my prefix would then be site_
the first site in the network would then be site_1
ie site_1_posts
. So in this instance, that'd be what @AlexECP was suggesting - if you want to reach the posts for a site in your network, it appears, with this solution that you'd have to change the prefix on the fly in order to "select" which site in your network to query from on the fly.
from corcel.
I've settled with this solution, which seems to work for now:
class Post extends Corcel
{
protected $connection = 'wordpress';
protected $postType = 'post';
public function __construct($whichSite) {
$this->connection = $whichSite;
}
}
Then I just set multiple connections inside of config/database.php
and call them upon the class invocation.
$main = new \App\Post('wordpress');
$main->published()->get();
$adelaide = new \App\Post('adelaide');
$adelaide->published()->get();
Not sure if this method is just sluggish or going to cause multiple connections / other issues? Would be keen to hear from someone else attempting this.
from corcel.
I think having to setup multiple connection inside of config/database.php
is not realistic for a large multisite installation. When I need to edit batches of sites just use do the following:
$post = new \App\Post;
foreach (\App\Website::get() as $website){
$post->getConnection()->setTablePrefix('wp_'.$website->wordpress_site_id.'_');
...logic...
}
I also sometimes create static functions that take the wordpress site id as a parameter, and start with:
public static function sample($wordpress_site_id){
$post = new self;
$post->getConnection()->setTablePrefix('wp_'.$wordpress_site_id.'_');
...
}
from corcel.
Hy @AharonFeinstein thanks for the contribution. I guess maybe it's better to create a scopeNetwork()
for that, receiving an ID or website name:
$posts = Post::network(123)->where(..)->get();
Actually there's no official support for multisite into Corcel but of course, that would be an excellent contribution! Be welcome!
from corcel.
Hi @jgrossi, thanks for writing Corcel!
It's perfect for the project we're about to work on, except that we definitely need multi-site functionality. I will use some of the suggestions above to start with, but I'd also be keen to put some work into extending Corcel to do it natively.
But before I do, I'd love to know your thoughts on how best to do this.
from corcel.
Hi @bluehaoran thanks for the kind words. About multi-site, Corcel works as a library, a composer package. There's no way to work with multiple websites using just a single database connection, but you can have as many database connections you want to, changing the prefix for each one. So using something like Post::on('website2')->all()
.
Would this help?
from corcel.
@jgrossi thanks for taking the time to reply.
As I see it, there are two problems to solve with multi-site:
- It feels inefficient to use two connections--one with a 'wp_' prefix and one a wp_X_' prefix. In the same request, I might well want access to wp_users, wp_options, and wp_2_posts and wp_2_postmeta.
I'm thinking that in this circumstance, you'd tweak the logic around generating the $table property of Corcel\Model\Post
and Corcel\Model\User
etc.
- A separate, more difficult but less frequent problem is when I need access to something form wp_2_posts and wp_3_posts in the same request. This is the case where having multiple connections seems most sensible, although I like your suggestion above for using Scopes to apply the
getConnection()->setTablePrefix()
trick explained above.
Is that sensible?
from corcel.
@bluehaoran actually you can access both database connections in the same request. The on()
method on Post::on($connection)
would access that connection and the wp_posts
table for that specific connection:
$posts = Post::on('site_1')->all(); // posts from `wp_posts`
$posts = Post::on('site_2')->all(); // posts from `wp_2_posts`
Just to let things clear, the on()
is not a scope actually, it's a Eloquent method that change from which connection you're accessing from.
from corcel.
If you're on this thread, here's a little bug I picked up with Connections in Laravel (laravel/framework#26589). This mainly became an issue for me writing unit tests, but YMMV.
from corcel.
@bluehaoran actually, about the laravel/framework#26589 that's the expected behavior. can you explain better which test you wrote?
from corcel.
Related Issues (20)
- Trying to get property 'guid' of non-object.
- Corcel and Laravel Voyager incompatibility HOT 1
- How to use corcel in WordPress plugin for custom tables
- Get post category name HOT 1
- How to use WordPress plugins inside Laravel with Corcel? HOT 4
- Multiple Database connection, What should I set connection in Corcel ? HOT 1
- Two factor authentification HOT 2
- Menu doesn't return Product Categories added to WordPress menus
- How to query for Posts with updated_at field ?
- Corcel acf plugin not installable
- Missing PhpOption\Option dependencies on CakePHP HOT 2
- DB connection error Laravel HOT 1
- Add support for Laravel 10 HOT 10
- onEachSide give error but works HOT 1
- Project abandoned? HOT 3
- The Eloquent model's deprecated $dates
- Laravel Nova & Corcel - many-to-many relation
- Getting error unserialize(): Error at offset 0 of 2 bytes - using Carbon Fields
- As A developer I want Documentation on how to upload, create, save and update Media to The Media Library So it's done it as intended HOT 1
- Laravel 11 and PHP 8.2 support HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from corcel.